home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / T / TE32K Folder / TE32K.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-11  |  79.0 KB  |  3,193 lines  |  [TEXT/KAHL]

  1. #include    "TE32K.h"
  2.  
  3. #define    EXTRALINESTARTS        32L
  4. #define    EXTRATEXTBUFF        256L
  5.  
  6. #define    LEFTARROW            28
  7. #define    RIGHTARROW            29
  8. #define UPARROW                30
  9. #define DOWNARROW            31
  10. #define    TAB                    '\t'
  11. #define DELETE                8
  12. #define    RETURN                13
  13. #define    ENTER                3
  14.  
  15.  
  16.  
  17. Handle            TE32KScrpHandle = 0L;
  18. long            TE32KScrpLength = 0L;
  19. TE32KHandle        clickedTE32KH = 0L;
  20.  
  21.  
  22.  
  23. static long        LineEndIndex(long,TE32KHandle);
  24. static void        CalParagraph(long,TE32KHandle,long *,long *);
  25. static long     paraLines(long,TE32KHandle);
  26. static void     updateLine(long,TE32KHandle,int,LongRect *);
  27. static void        invertSelRange(long,long,TE32KHandle);
  28. static void        xorCaret(TE32KHandle);
  29. static long        indexToLine(long,TE32KHandle);
  30. static int        shiftKeyDown(void);
  31. static void     MyClicker(void);
  32. static void     MyClickLoop(void);
  33.  
  34.  
  35.  
  36.  
  37. void    TE32KSetFontStuff(int txFont,int txFace,int txMode,int txSize,TE32KHandle theTE32KHandle)
  38. {
  39. register int        i;
  40. int                    oldFont,oldFace,oldSize,oldMode;
  41. GrafPtr                oldPort;
  42. FontInfo            theFontInfo;
  43.  
  44.  
  45.     (**theTE32KHandle).txFont = txFont;
  46.     (**theTE32KHandle).txFace = txFace;
  47.     (**theTE32KHandle).txMode = txMode;
  48.     (**theTE32KHandle).txSize = txSize;
  49.     
  50.     GetPort(&oldPort);
  51.     SetPort((**theTE32KHandle).inPort);
  52.     oldFont = ((**theTE32KHandle).inPort)->txFont;
  53.     oldFace = ((**theTE32KHandle).inPort)->txFace;
  54.     oldSize = ((**theTE32KHandle).inPort)->txSize;
  55.     oldMode = ((**theTE32KHandle).inPort)->txMode;
  56.     
  57.     TextFont((**theTE32KHandle).txFont);
  58.     TextFace((**theTE32KHandle).txFace);
  59.     TextSize((**theTE32KHandle).txSize);
  60.     TextMode((**theTE32KHandle).txMode);
  61.     
  62.     
  63.     for (i=0;i<256;i++)
  64.         (**theTE32KHandle).theCharWidths[i] = CharWidth((unsigned char) i);
  65.     
  66.     GetFontInfo(&theFontInfo);
  67.     
  68.     (**theTE32KHandle).lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
  69.     (**theTE32KHandle).fontAscent = theFontInfo.ascent;
  70.     
  71.     if ((**theTE32KHandle).tabChars)
  72.         (**theTE32KHandle).tabWidth = (**theTE32KHandle).tabChars * (**theTE32KHandle).theCharWidths[' '];
  73.     
  74.     TextFont(oldFont);
  75.     TextFace(oldFace);
  76.     TextSize(oldSize);
  77.     TextMode(oldMode);
  78.     
  79.     SetPort(oldPort);
  80. }
  81.  
  82.  
  83.  
  84.  
  85.  
  86. void    SetLongRect(LongRect *theLongRect,long left,long top,long right,long bottom)
  87. {
  88.     theLongRect->left = left;
  89.     theLongRect->top = top;
  90.     theLongRect->right = right;
  91.     theLongRect->bottom = bottom;
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98. void    RectToLongRect(Rect *theRect,LongRect *theLongRect)
  99. {
  100.     theLongRect->left = (long) theRect->left;
  101.     theLongRect->top = (long) theRect->top;
  102.     theLongRect->right = (long) theRect->right;
  103.     theLongRect->bottom = (long) theRect->bottom;
  104. }
  105.  
  106.  
  107.  
  108.  
  109. void    LongRectToRect(LongRect *theLongRect,Rect *theRect)
  110. {
  111.     if (theLongRect->left < -32768L)
  112.         theRect->left = (int) -32768;
  113.     else if (theLongRect->left > 32767L)
  114.         theRect->left = (int) 32767;
  115.     else
  116.         theRect->left = (int) theLongRect->left;
  117.     
  118.     if (theLongRect->top < -32768L)
  119.         theRect->top = (int) -32768;
  120.     else if (theLongRect->top > 32767L)
  121.         theRect->top = (int) 32767;
  122.     else
  123.         theRect->top = (int) theLongRect->top;
  124.     
  125.     if (theLongRect->right < -32768L)
  126.         theRect->right = (int) -32768;
  127.     else if (theLongRect->right > 32767L)
  128.         theRect->right = (int) 32767;
  129.     else
  130.         theRect->right = (int) theLongRect->right;
  131.     
  132.     if (theLongRect->bottom < -32768L)
  133.         theRect->bottom = (int) -32768;
  134.     else if (theLongRect->bottom > 32767L)
  135.         theRect->bottom = (int) 32767;
  136.     else
  137.         theRect->bottom = (int) theLongRect->bottom;
  138. }
  139.  
  140.  
  141.  
  142. void    OffsetLongRect(LongRect *theLongRect, long x, long y)
  143. {
  144.         theLongRect->left += x;
  145.         theLongRect->top += y;
  146.         theLongRect->right += x;
  147.         theLongRect->bottom += y;
  148. }
  149.  
  150.  
  151.  
  152.  
  153. static long    indexToLine(long selIndex,TE32KHandle theTE32KHandle)
  154. {
  155. register long    i,delta;
  156.  
  157.     if (theTE32KHandle)
  158.     {
  159.         if (selIndex<=0L || (**theTE32KHandle).nLines<=1L || (**theTE32KHandle).teLength<1L)
  160.             return(0L);
  161.         
  162.         else if (selIndex >= (**theTE32KHandle).teLength)
  163.             return((long) ((**theTE32KHandle).nLines - 1L));
  164.         
  165.         else
  166.         {
  167.             i = ((**theTE32KHandle).nLines) >> 1;
  168.             
  169.             delta = ((**theTE32KHandle).nLines) >> 1;
  170.             if (delta < 1L)
  171.                 delta = 1L;
  172.             
  173.             while (delta > 0L)
  174.             {
  175.                 if (selIndex == (**theTE32KHandle).lineStarts[i])
  176.                     delta = 0L;
  177.                 
  178.                 else if (selIndex > (**theTE32KHandle).lineStarts[i])
  179.                 {
  180.                     if (selIndex < (**theTE32KHandle).lineStarts[i+1])
  181.                         delta = 0L;
  182.                     
  183.                     else
  184.                         i += delta;
  185.                 }
  186.                 
  187.                 else
  188.                     i -= delta;
  189.                 
  190.                 if (delta)
  191.                 {
  192.                     delta >>= 1;
  193.                     
  194.                     if (delta < 1L)
  195.                         delta = 1L;
  196.                 }
  197.             }
  198.         }
  199.         
  200.         if (i < 0L)
  201.             i = 0L;
  202.         else if (i >= (**theTE32KHandle).nLines)
  203.             i = (**theTE32KHandle).nLines - 1L;
  204.         
  205.         return((long) i);
  206.     }
  207.     
  208.     else
  209.         return((long) 0L);
  210. }
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218. static void    xorCaret(TE32KHandle theTE32KHandle)
  219. {
  220. GrafPtr            oldPort;
  221. PenState        oldPenState;
  222. Point            selPt;
  223. RgnHandle        oldClipRgn;
  224. Rect            theClipRect;
  225.  
  226.     if (theTE32KHandle && (**theTE32KHandle).active && (**theTE32KHandle).selStart==(**theTE32KHandle).selEnd)
  227.     {
  228.         if (!(**theTE32KHandle).caretState && ((**theTE32KHandle).selStart < 0 || (**theTE32KHandle).selEnd > (**theTE32KHandle).teLength))
  229.             return;
  230.         
  231.         GetPort(&oldPort);
  232.         SetPort((**theTE32KHandle).inPort);
  233.         
  234.         GetPenState(&oldPenState);
  235.         oldClipRgn = NewRgn();
  236.         GetClip(oldClipRgn);
  237.         
  238.         theClipRect.left = (int) ((**theTE32KHandle).viewRect.left);
  239.         theClipRect.top = (int) ((**theTE32KHandle).viewRect.top);
  240.         theClipRect.right = (int) ((**theTE32KHandle).viewRect.right);
  241.         theClipRect.bottom = (int) ((**theTE32KHandle).viewRect.bottom);
  242.         
  243.         ClipRect(&theClipRect);
  244.         
  245.         PenNormal();
  246.         
  247.         PenMode(patXor);
  248.         
  249.         if ((**theTE32KHandle).selPoint.h < -32768L)
  250.             selPt.h = (int) -32768;
  251.         else if ((**theTE32KHandle).selPoint.h > 32767L)
  252.             selPt.h = (int) 32767;
  253.         else
  254.             selPt.h = (int) (**theTE32KHandle).selPoint.h;
  255.         
  256.         if ((**theTE32KHandle).selPoint.v < -32768L)
  257.             selPt.v = (int) -32768;
  258.         else if ((**theTE32KHandle).selPoint.v > 32767L)
  259.             selPt.v = (int) 32767;
  260.         else
  261.             selPt.v = (int) (**theTE32KHandle).selPoint.v;
  262.         
  263.         MoveTo(selPt.h - 1,selPt.v);
  264.         Line(0,-(**theTE32KHandle).fontAscent);
  265.         
  266.         (**theTE32KHandle).caretTime = TickCount() + GetCaretTime();
  267.         (**theTE32KHandle).caretState = !(**theTE32KHandle).caretState;
  268.         
  269.         SetClip(oldClipRgn);
  270.         DisposeRgn(oldClipRgn);
  271.         
  272.         SetPenState(&oldPenState);
  273.         SetPort(oldPort);
  274.     }
  275. }
  276.  
  277.  
  278.  
  279.  
  280. void    TE32KInit()
  281. {
  282.     TE32KScrpHandle = NewHandle(0L);
  283.     TE32KScrpLength = 0L;
  284. }
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291. TE32KHandle        TE32KNew(LongRect *destRect,LongRect *viewRect)
  292. {
  293. TE32KHandle        newTE32KHandle;
  294. Handle            hText;
  295. GrafPtr            activePort;
  296. FontInfo        theFontInfo;
  297. LongPoint        selPt;
  298.  
  299.     newTE32KHandle = (TE32KHandle) NewHandle((long) sizeof(TE32KRec) + (long) sizeof(long)*EXTRALINESTARTS);
  300.     if (MemError() || StripAddress(newTE32KHandle)==0L)
  301.         return((TE32KHandle) 0L);
  302.     
  303.     hText = NewHandle(EXTRATEXTBUFF);
  304.     if (MemError() || StripAddress(hText)==0L)
  305.     {
  306.         DisposHandle((Handle) newTE32KHandle);
  307.         return((TE32KHandle) 0L);
  308.     }
  309.     
  310.     (**newTE32KHandle).destRect = *destRect;
  311.     (**newTE32KHandle).viewRect = *viewRect;
  312.     
  313.     GetPort(&activePort);
  314.     GetFontInfo(&theFontInfo);
  315.     
  316.     (**newTE32KHandle).lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
  317.     (**newTE32KHandle).fontAscent = theFontInfo.ascent;
  318.     
  319.     (**newTE32KHandle).selStart = 0L;
  320.     (**newTE32KHandle).selEnd = 0L;
  321.     
  322.     (**newTE32KHandle).teLength = 0L;
  323.     (**newTE32KHandle).hText = hText;
  324.     
  325.     (**newTE32KHandle).txFont = activePort->txFont;
  326.     (**newTE32KHandle).txFace = activePort->txFace;
  327.     (**newTE32KHandle).txMode = activePort->txMode;
  328.     (**newTE32KHandle).txSize = activePort->txSize;
  329.     
  330.     (**newTE32KHandle).inPort = activePort;
  331.     
  332.     (**newTE32KHandle).tabWidth = 25;
  333.     (**newTE32KHandle).tabChars = 0;
  334.     
  335.     (**newTE32KHandle).maxLineWidth = 32767;
  336.     
  337.     (**newTE32KHandle).clikStuff = FALSE;
  338.     
  339.     (**newTE32KHandle).crOnly = 0x00;
  340.     
  341.     (**newTE32KHandle).nLines = 1L;
  342.     (**newTE32KHandle).lineStarts[0] = 0L;
  343.     (**newTE32KHandle).lineStarts[1] = 0L;
  344.     
  345.     (**newTE32KHandle).active = TRUE;
  346.     (**newTE32KHandle).caretState = FALSE;
  347.     (**newTE32KHandle).caretTime = TickCount();
  348.     
  349.     (**newTE32KHandle).clickTime = TickCount();
  350.     (**newTE32KHandle).clickLoc = -1L;
  351.     
  352.     TE32KGetPoint((**newTE32KHandle).selStart,&selPt,newTE32KHandle);
  353.     
  354.     (**newTE32KHandle).selPoint = selPt;
  355.     
  356.     (**newTE32KHandle).clikLoop = 0L;
  357.     
  358.     TE32KSetFontStuff((**newTE32KHandle).txFont,(**newTE32KHandle).txFace,(**newTE32KHandle).txMode,(**newTE32KHandle).txSize,newTE32KHandle);
  359.     
  360.     return((TE32KHandle) newTE32KHandle);
  361. }
  362.  
  363.  
  364.  
  365.  
  366.  
  367. void TE32KDispose(TE32KHandle theTE32KHandle)
  368. {
  369.     if (theTE32KHandle)
  370.     {
  371.         if ((**theTE32KHandle).hText)
  372.             DisposHandle((**theTE32KHandle).hText);
  373.         
  374.         DisposHandle(theTE32KHandle);
  375.     }
  376. }
  377.  
  378.  
  379.  
  380.  
  381. void    TE32KCalText(TE32KHandle theTE32KHandle)
  382. {
  383. register unsigned char    *charPtr;
  384. register long            charCount;
  385. register int            *theCharWidths,lineLength,crOnly,maxLineWidth;
  386. register unsigned char    ch;
  387. long                    nLines,maxLineStarts,sizeTE32KHandle;
  388. unsigned char            *charBase;
  389. Point                    cursorPt;
  390. int                        rightSide,destLeftSide,tabWidth,maxRewind;
  391. unsigned char            *oldCharPtr;
  392. long                    oldCharCount,tempOffset;
  393.  
  394.     if (theTE32KHandle)
  395.     {
  396.         (**theTE32KHandle).lineStarts[0] = 0L;        /* assume the worst can happen and prepare for it */
  397.         (**theTE32KHandle).lineStarts[1] = 0L;
  398.         (**theTE32KHandle).nLines = 1L;
  399.         
  400.         sizeTE32KHandle  = GetHandleSize((Handle) theTE32KHandle);
  401.         maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  402.         
  403.         crOnly = (**theTE32KHandle).crOnly;
  404.         maxLineWidth = (**theTE32KHandle).maxLineWidth;
  405.         
  406.         lineLength = 0L;
  407.         nLines = 0L;
  408.         
  409.         charBase = (unsigned char *) *((**theTE32KHandle).hText);
  410.         charPtr = charBase;
  411.         charCount = (**theTE32KHandle).teLength;
  412.         
  413.         
  414.         if (charCount > 0L)
  415.         {
  416.             rightSide = (int) ((**theTE32KHandle).destRect.right);
  417.             destLeftSide = (int) ((**theTE32KHandle).destRect.left + 1L);
  418.             cursorPt.h = destLeftSide;
  419.             tabWidth = (long) (**theTE32KHandle).tabWidth;
  420.             
  421.             theCharWidths = (**theTE32KHandle).theCharWidths;
  422.             
  423.             while (charCount--)
  424.             {
  425.                 ch = *charPtr++;
  426.                 lineLength++;
  427.                 
  428.                 if (!crOnly)
  429.                 {
  430.                     if (ch == TAB)
  431.                         cursorPt.h = destLeftSide + ((cursorPt.h - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  432.                     else if (ch != '\r' && ch != '\n')
  433.                         cursorPt.h += theCharWidths[ch];
  434.                 }
  435.                 
  436.                 if ((ch == '\r' || ch == '\n') || (ch != ' ' && cursorPt.h >= rightSide) || (!crOnly && lineLength > maxLineWidth))
  437.                 {
  438.                     if ((ch != ' ' && cursorPt.h >= rightSide) || (!crOnly && lineLength > maxLineWidth))
  439.                     {
  440.                         /* I should probably add a hook for custom word-breaking */
  441.                         
  442.                         maxRewind = charPtr - charBase - (**theTE32KHandle).lineStarts[nLines];
  443.                         oldCharPtr = charPtr;
  444.                         oldCharCount = charCount;
  445.                         
  446.                         charPtr--;
  447.                         charCount++;
  448.                         maxRewind--;
  449.                         
  450.                         while (*charPtr != ' ' && maxRewind > 0)
  451.                         {
  452.                             charPtr--;
  453.                             charCount++;
  454.                             maxRewind--;
  455.                         }
  456.                         
  457.                         if (maxRewind <= 0)
  458.                         {
  459.                             charPtr = oldCharPtr;
  460.                             charCount = oldCharCount;
  461.                         }
  462.                         
  463.                         else
  464.                         {
  465.                             charPtr++;
  466.                             charCount--;
  467.                         }
  468.                     }
  469.                     
  470.                     if (nLines >= maxLineStarts)
  471.                     {
  472.                         tempOffset = charPtr - charBase;
  473.                                                 
  474.                         sizeTE32KHandle = (long) sizeof(TE32KRec) + (long) sizeof(long)*(nLines + EXTRALINESTARTS);
  475.                         maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  476.                         
  477.                         SetHandleSize((Handle) theTE32KHandle,sizeTE32KHandle);
  478.                         
  479.                         if (MemError())
  480.                             return;
  481.                         
  482.                         charBase = (unsigned char *) *((**theTE32KHandle).hText);
  483.                         charPtr = charBase + tempOffset;
  484.                         theCharWidths = (**theTE32KHandle).theCharWidths;
  485.                     }
  486.                     
  487.                     (**theTE32KHandle).lineStarts[++nLines] = charPtr - charBase;
  488.                     
  489.                     cursorPt.h = destLeftSide;
  490.                     lineLength = 0L;
  491.                 }
  492.             }
  493.             
  494.             if (nLines >= maxLineStarts)
  495.             {
  496.                 sizeTE32KHandle = (long) sizeof(TE32KRec) + (long) sizeof(long)*(nLines + EXTRALINESTARTS);
  497.                 
  498.                 SetHandleSize((Handle) theTE32KHandle,sizeTE32KHandle);
  499.                 
  500.                 if (MemError())
  501.                     return;
  502.             }
  503.             
  504.             (**theTE32KHandle).lineStarts[++nLines] = charPtr - charBase;
  505.             
  506.             (**theTE32KHandle).nLines = nLines;
  507.         }
  508.     }
  509. }
  510.  
  511.  
  512.  
  513.  
  514.  
  515. void    TE32KUseTextHandle(Handle hText,TE32KHandle theTE32KHandle)
  516. {
  517. LongPoint    selPt;
  518. long int textLength;
  519.  
  520.     if (theTE32KHandle)
  521.     {
  522.         textLength = GetHandleSize(hText);
  523.  
  524.         if ((**theTE32KHandle).hText)
  525.             DisposHandle((**theTE32KHandle).hText);
  526.                 
  527.         (**theTE32KHandle).hText = hText;
  528.         (**theTE32KHandle).teLength = textLength;
  529.         
  530.         (**theTE32KHandle).selStart = textLength;
  531.         (**theTE32KHandle).selEnd = textLength;
  532.         
  533.         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  534.         (**theTE32KHandle).selPoint = selPt;
  535.         
  536.         TE32KCalText(theTE32KHandle);
  537.     }
  538. }
  539.  
  540.  
  541.  
  542.  
  543.  
  544. void    TE32KSetText(Ptr textPtr,long textLength,TE32KHandle theTE32KHandle)
  545. {
  546. Handle        hText;
  547. LongPoint    selPt;
  548.  
  549.     if (theTE32KHandle)
  550.     {
  551.         hText = NewHandle(textLength + EXTRATEXTBUFF);
  552.         
  553.         if (MemError() || StripAddress(hText)==0L)
  554.             return;
  555.         
  556.         if ((**theTE32KHandle).hText)
  557.             DisposHandle((**theTE32KHandle).hText);
  558.         
  559.         HLock(hText);
  560.         BlockMove(textPtr,*hText,textLength);
  561.         HUnlock(hText);
  562.         
  563.         (**theTE32KHandle).hText = hText;
  564.         (**theTE32KHandle).teLength = textLength;
  565.         
  566.         (**theTE32KHandle).selStart = textLength;
  567.         (**theTE32KHandle).selEnd = textLength;
  568.         
  569.         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  570.         (**theTE32KHandle).selPoint = selPt;
  571.         
  572.         TE32KCalText(theTE32KHandle);
  573.     }
  574. }
  575.  
  576.  
  577.  
  578.  
  579. Handle    TE32KGetText(TE32KHandle theTE32KHandle)
  580. {
  581.     if (theTE32KHandle)
  582.         return((Handle) (**theTE32KHandle).hText);
  583.     else
  584.         return((Handle) 0L);
  585. }
  586.  
  587.  
  588.  
  589.  
  590. void    TE32KUpdate(LongRect *updateLongRect,TE32KHandle theTE32KHandle)
  591. {
  592. LongRect                    tempLongRect;
  593. Rect                        theClipRect,viewRect,updateRect;
  594. GrafPtr                        oldPort,currentPort;
  595. RgnHandle                    oldClipRgn;
  596. register unsigned char        *textPtr;
  597. register long                firstLine,lastLine,i,thisStart,nextStart,tabWidth;
  598. Point                        cursorPt;
  599. int                            oldFont,oldFace,oldSize,oldMode;
  600. int                            rightSide,destLeftSide;
  601. LongPoint                    selPt;
  602. unsigned char                oldCaretState;
  603.  
  604.  
  605.     if (theTE32KHandle && (**theTE32KHandle).inPort)
  606.     {
  607.         tempLongRect = (**theTE32KHandle).viewRect;
  608.         LongRectToRect(&tempLongRect,&viewRect);
  609.         
  610.         tempLongRect = *updateLongRect;
  611.         tempLongRect.top = (**theTE32KHandle).destRect.top + ((updateLongRect->top - (**theTE32KHandle).destRect.top)/(**theTE32KHandle).lineHeight)*(**theTE32KHandle).lineHeight;
  612.         tempLongRect.bottom = (**theTE32KHandle).destRect.top + ((updateLongRect->bottom - (**theTE32KHandle).destRect.top + (**theTE32KHandle).lineHeight - 1L)/(**theTE32KHandle).lineHeight)*(**theTE32KHandle).lineHeight;
  613.         
  614.         LongRectToRect(&tempLongRect,&updateRect);
  615.         
  616.         if (SectRect(&viewRect,&updateRect,&theClipRect))
  617.         {
  618.             GetPort(&oldPort);
  619.             currentPort = (**theTE32KHandle).inPort;
  620.             SetPort(currentPort);
  621.             
  622.             oldClipRgn = NewRgn();
  623.             GetClip(oldClipRgn);
  624.             ClipRect(&theClipRect);
  625.             
  626.             oldCaretState = (**theTE32KHandle).caretState;
  627.             
  628.             if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd && oldCaretState)
  629.                 xorCaret(theTE32KHandle);
  630.             
  631.             firstLine = ((long) theClipRect.top - (**theTE32KHandle).destRect.top)/(long) (**theTE32KHandle).lineHeight;
  632.             lastLine = ((long) theClipRect.bottom - (**theTE32KHandle).destRect.top - 1L)/(long) (**theTE32KHandle).lineHeight;
  633.             
  634.             if (firstLine < 0)
  635.                 firstLine = 0;
  636.             
  637.             if (lastLine >= (**theTE32KHandle).nLines)
  638.                 lastLine = (**theTE32KHandle).nLines - 1L;
  639.             
  640.             if (firstLine > lastLine)
  641.                 lastLine = firstLine;
  642.             
  643.             EraseRect(&theClipRect);
  644.             
  645.             if (firstLine < (**theTE32KHandle).nLines && (**theTE32KHandle).teLength > 0L)
  646.             {
  647.                 rightSide = theClipRect.right;
  648.                 destLeftSide = (int) (**theTE32KHandle).destRect.left + 1L;
  649.                 cursorPt.h = destLeftSide;
  650.                 cursorPt.v = (int) ((**theTE32KHandle).destRect.top + firstLine * (long) (**theTE32KHandle).lineHeight + (long) (**theTE32KHandle).fontAscent);
  651.                 
  652.                 oldFont = ((**theTE32KHandle).inPort)->txFont;
  653.                 oldFace = ((**theTE32KHandle).inPort)->txFace;
  654.                 oldSize = ((**theTE32KHandle).inPort)->txSize;
  655.                 oldMode = ((**theTE32KHandle).inPort)->txMode;
  656.                 
  657.                 TextFont((**theTE32KHandle).txFont);
  658.                 TextFace((**theTE32KHandle).txFace);
  659.                 TextSize((**theTE32KHandle).txSize);
  660.                 TextMode((**theTE32KHandle).txMode);
  661.                 
  662.                 HLock((**theTE32KHandle).hText);
  663.                 
  664.                 textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  665.                 tabWidth = (long) (**theTE32KHandle).tabWidth;
  666.                 
  667.                 while (firstLine <= lastLine)
  668.                 {
  669.                     thisStart = (**theTE32KHandle).lineStarts[firstLine];
  670.                     i = thisStart;
  671.                     
  672.                     nextStart = (**theTE32KHandle).lineStarts[firstLine+1];
  673.                     
  674.                     if (nextStart > thisStart && (textPtr[nextStart-1] == '\r' || textPtr[nextStart-1] == '\n'))
  675.                         nextStart--;
  676.                     
  677.                     MoveTo(cursorPt.h,cursorPt.v);
  678.                     
  679.                     while (thisStart < nextStart)
  680.                     {
  681.                         while (i<nextStart && textPtr[i]!=TAB)
  682.                             i++;
  683.                         
  684.                         if (i > thisStart)
  685.                             DrawText(&(textPtr[thisStart]),0,(int) (i - thisStart));
  686.                         
  687.                         if (i<nextStart && textPtr[i]==TAB)
  688.                         {
  689.                             MoveTo(destLeftSide + ((currentPort->pnLoc.h - destLeftSide + tabWidth)/tabWidth)*tabWidth,currentPort->pnLoc.v);
  690.                             i++;
  691.                         }
  692.                         
  693.                         thisStart = i;
  694.                         
  695.                         if (currentPort->pnLoc.h > theClipRect.right)
  696.                             thisStart = nextStart;
  697.                     }
  698.                     
  699.                     firstLine++;
  700.                     cursorPt.v += (**theTE32KHandle).lineHeight;
  701.                 }
  702.                 
  703.                 HUnlock((**theTE32KHandle).hText);
  704.                 
  705.                 TextFont(oldFont);
  706.                 TextFace(oldFace);
  707.                 TextSize(oldSize);
  708.                 TextMode(oldMode);
  709.             }
  710.             
  711.             if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  712.                 invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  713.             
  714.             else
  715.             {
  716.                 TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  717.                 (**theTE32KHandle).selPoint = selPt;
  718.                 
  719.                 if (oldCaretState)
  720.                     xorCaret(theTE32KHandle);
  721.             }
  722.             
  723.             SetClip(oldClipRgn);
  724.             DisposeRgn(oldClipRgn);
  725.             
  726.             SetPort(oldPort);
  727.         }
  728.     }
  729. }
  730.  
  731.  
  732.  
  733.  
  734.  
  735. void    TE32KScroll(long horiz,long vert,TE32KHandle theTE32KHandle)
  736. {
  737. LongRect    updateLongRect;
  738. Rect        scrollRect;
  739. RgnHandle    updateRgn;
  740. GrafPtr        oldPort;
  741. LongPoint    selPt;
  742.  
  743.     if (theTE32KHandle && (**theTE32KHandle).inPort && (horiz || vert))
  744.     {
  745.         GetPort(&oldPort);
  746.         SetPort((**theTE32KHandle).inPort);
  747.         
  748.         (**theTE32KHandle).destRect.left += horiz;
  749.         (**theTE32KHandle).destRect.top += vert;
  750.         (**theTE32KHandle).destRect.right += horiz;
  751.         (**theTE32KHandle).destRect.bottom += vert;
  752.         
  753.         (**theTE32KHandle).selPoint.h += horiz;
  754.         (**theTE32KHandle).selPoint.v += vert;
  755.         selPt = (**theTE32KHandle).selPoint;
  756.         
  757.         scrollRect.left = (int) ((**theTE32KHandle).viewRect.left);
  758.         scrollRect.top = (int) ((**theTE32KHandle).viewRect.top);
  759.         scrollRect.right = (int) ((**theTE32KHandle).viewRect.right);
  760.         scrollRect.bottom = (int) ((**theTE32KHandle).viewRect.bottom);
  761.         
  762.         if (horiz < ((**theTE32KHandle).viewRect.right-(**theTE32KHandle).viewRect.left) ||
  763.             vert < ((**theTE32KHandle).viewRect.bottom-(**theTE32KHandle).viewRect.top))
  764.         {
  765.             updateRgn = NewRgn();
  766.             
  767.             ScrollRect(&scrollRect,(int) horiz,(int) vert,updateRgn);
  768.             
  769.             updateLongRect.left = (**updateRgn).rgnBBox.left;
  770.             updateLongRect.top = (**updateRgn).rgnBBox.top;
  771.             updateLongRect.right = (**updateRgn).rgnBBox.right;
  772.             updateLongRect.bottom = (**updateRgn).rgnBBox.bottom;
  773.             
  774.             DisposeRgn(updateRgn);
  775.             
  776.             TE32KUpdate(&updateLongRect,theTE32KHandle);
  777.             
  778.             if ((**theTE32KHandle).caretState)
  779.                 xorCaret(theTE32KHandle);
  780.             
  781.             (**theTE32KHandle).selPoint = selPt;
  782.             
  783.             xorCaret(theTE32KHandle);
  784.         }
  785.         
  786.         else
  787.         {
  788.             updateLongRect = (**theTE32KHandle).viewRect;
  789.             
  790.             TE32KUpdate(&updateLongRect,theTE32KHandle);
  791.             
  792.             if ((**theTE32KHandle).caretState)
  793.                 xorCaret(theTE32KHandle);
  794.             
  795.             (**theTE32KHandle).selPoint = selPt;
  796.             
  797.             xorCaret(theTE32KHandle);
  798.         }
  799.         
  800.         SetPort(oldPort);
  801.     }
  802. }
  803.  
  804.  
  805.  
  806.  
  807.  
  808. void    TE32KActivate(TE32KHandle theTE32KHandle)
  809. {
  810.     if (theTE32KHandle && !((**theTE32KHandle).active))
  811.     {
  812.         (**theTE32KHandle).active = TRUE;
  813.         (**theTE32KHandle).caretState = FALSE;
  814.         
  815.         invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  816.     }
  817. }
  818.  
  819.  
  820.  
  821.  
  822.  
  823. void    TE32KIdle(TE32KHandle theTE32KHandle)
  824. {
  825.     if (theTE32KHandle && (**theTE32KHandle).active && TickCount() >= (**theTE32KHandle).caretTime)
  826.     {
  827.         if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd)
  828.             xorCaret(theTE32KHandle);
  829.     }
  830. }
  831.  
  832.  
  833.  
  834.  
  835.  
  836. void    TE32KDeactivate(TE32KHandle theTE32KHandle)
  837. {
  838.     if (theTE32KHandle && (**theTE32KHandle).active)
  839.     {
  840.         if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd)
  841.         {
  842.             if ((**theTE32KHandle).caretState)
  843.                 xorCaret(theTE32KHandle);
  844.         }
  845.         else
  846.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  847.         
  848.         (**theTE32KHandle).active = FALSE;
  849.     }
  850. }
  851.  
  852.  
  853.  
  854. void    TE32KGetPoint(long selIndex,LongPoint *selPt,TE32KHandle theTE32KHandle)
  855. {
  856. register unsigned char    *textPtr;
  857. register int            *theCharWidths;
  858. register long            i,thisStart,tabWidth;
  859. long                    x,y,lineIndex,destLeftSide;
  860. unsigned char            ch;
  861. LongPoint                origPt;
  862. int                        clikStuff;
  863.  
  864.     if (theTE32KHandle)
  865.     {
  866.         if (selIndex<=0L || (**theTE32KHandle).teLength<1L)
  867.         {
  868.             selPt->h = (**theTE32KHandle).destRect.left + 1L;
  869.             selPt->v = (**theTE32KHandle).destRect.top + (**theTE32KHandle).fontAscent;
  870.             (**theTE32KHandle).clikStuff = FALSE;
  871.             
  872.             return;
  873.         }
  874.         
  875.         clikStuff = (**theTE32KHandle).clikStuff;
  876.         (**theTE32KHandle).clikStuff = FALSE;
  877.         
  878.         origPt = *selPt;
  879.         
  880.         i = indexToLine(selIndex,theTE32KHandle);
  881.         
  882.         y = (**theTE32KHandle).destRect.top + ((**theTE32KHandle).lineHeight * i) + (**theTE32KHandle).fontAscent;
  883.         
  884.         selPt->v = y;
  885.         
  886.         if (!(**theTE32KHandle).crOnly && clikStuff && i > 0 && selIndex == (**theTE32KHandle).lineStarts[i])
  887.         {
  888.             i--;
  889.             selPt->v -= (**theTE32KHandle).lineHeight;
  890.         }
  891.         
  892.         else if (selIndex < (**theTE32KHandle).lineStarts[i] || (selIndex == (**theTE32KHandle).lineStarts[i] && i < 1))
  893.         {
  894.             selPt->h = (**theTE32KHandle).destRect.left + 1L;
  895.             return;
  896.         }
  897.         
  898.         
  899.         HLock((**theTE32KHandle).hText);
  900.         
  901.         lineIndex  = i;
  902.         textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  903.         
  904.         destLeftSide = (**theTE32KHandle).destRect.left + 1L;
  905.         x = destLeftSide;
  906.         
  907.         thisStart = (**theTE32KHandle).lineStarts[lineIndex];
  908.         
  909.         theCharWidths = (**theTE32KHandle).theCharWidths;
  910.         
  911.         if (textPtr[selIndex-1] != '\r' && textPtr[selIndex-1] != '\n')
  912.         {
  913.             tabWidth = (long) (**theTE32KHandle).tabWidth;
  914.             
  915.             while (thisStart < selIndex)
  916.             {
  917.                 ch = textPtr[thisStart++];
  918.                 
  919.                 if (ch == TAB)
  920.                     x = destLeftSide + ((x - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  921.                 
  922.                 else
  923.                     x += theCharWidths[ch];
  924.             }
  925.         }
  926.         
  927.         HUnlock((**theTE32KHandle).hText);
  928.         
  929.         selPt->h = x;
  930.     }
  931. }
  932.  
  933.  
  934.  
  935.  
  936. long    TE32KGetOffset(LongPoint *selPt,TE32KHandle theTE32KHandle)
  937. {
  938. register unsigned char    *textPtr;
  939. register int            *theCharWidths;
  940. register long            i,delta,firstChar,lastChar,tabWidth;
  941. unsigned char            done;
  942. long                    x,y,selIndex,horiz,destLeftSide;
  943.  
  944.     if (theTE32KHandle)
  945.     {
  946.         if ((**theTE32KHandle).teLength < 1L)
  947.             return(0L);
  948.         
  949.         horiz = selPt->h;
  950.         
  951.         y = selPt->v - (**theTE32KHandle).destRect.top;
  952.         
  953.         i = y / (long) (**theTE32KHandle).lineHeight;
  954.         
  955.         if (i < 0L)
  956.             return(0L);
  957.         
  958.         if (i >= (**theTE32KHandle).nLines)
  959.             return((**theTE32KHandle).teLength);
  960.         
  961.         theCharWidths = (**theTE32KHandle).theCharWidths;
  962.         
  963.         HLock((**theTE32KHandle).hText);
  964.         
  965.         textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  966.         
  967.         destLeftSide = (**theTE32KHandle).destRect.left + 1L;
  968.         x = destLeftSide;
  969.         delta = 0L;
  970.         
  971.         firstChar = (**theTE32KHandle).lineStarts[i];
  972.         lastChar = (**theTE32KHandle).lineStarts[i+1L];
  973.         
  974.         tabWidth = (long) (**theTE32KHandle).tabWidth;
  975.         
  976.         if (firstChar<lastChar && x+delta<horiz)
  977.         {
  978.             done = FALSE;
  979.         
  980.             while (!done)
  981.             {
  982.                 if (textPtr[firstChar] != TAB)
  983.                     delta = (long) theCharWidths[textPtr[firstChar]];
  984.                 
  985.                 else
  986.                     delta = (destLeftSide + ((x - destLeftSide + tabWidth)/tabWidth)*tabWidth) - x;
  987.                 
  988.                 firstChar++;
  989.                 
  990.                 if (firstChar >= lastChar)
  991.                 {    
  992.                     if (textPtr[lastChar - 1L] == '\r' || textPtr[lastChar - 1L] == '\n')
  993.                         selIndex = lastChar - 1L;
  994.                     else
  995.                         selIndex = lastChar;
  996.                     
  997.                     done = TRUE;
  998.                 }
  999.                 
  1000.                 else if (x+delta >= horiz)
  1001.                 {
  1002.                     if (horiz >= x + (delta >> 1))
  1003.                         selIndex = firstChar;
  1004.                     else
  1005.                         selIndex = --firstChar;
  1006.                     
  1007.                     done = TRUE;
  1008.                 }
  1009.                 
  1010.                 else
  1011.                     x += delta;
  1012.             }
  1013.         }
  1014.         
  1015.         else
  1016.             selIndex = firstChar;
  1017.  
  1018.         HUnlock((**theTE32KHandle).hText);
  1019.         
  1020.         return(selIndex);
  1021.     }
  1022. }
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030. static void    invertSelRange(long selStart,long selEnd,TE32KHandle theTE32KHandle)
  1031. {
  1032. long        firstLine,lastLine;
  1033. Rect        viewRect,tempRect1,tempRect2,tempRect3,theRect;
  1034. LongPoint    selPt;
  1035. GrafPtr        oldPort;
  1036.  
  1037.  
  1038.     if (theTE32KHandle && (**theTE32KHandle).active)
  1039.     {
  1040.         if ((**theTE32KHandle).caretState)
  1041.             xorCaret(theTE32KHandle);
  1042.         
  1043.         if (selStart == selEnd)
  1044.         {
  1045.             TE32KGetPoint(selStart,&selPt,theTE32KHandle);
  1046.             (**theTE32KHandle).selPoint = selPt;
  1047.             (**theTE32KHandle).selStart = selStart;
  1048.             (**theTE32KHandle).selEnd = selStart;
  1049.             
  1050.             xorCaret(theTE32KHandle);
  1051.             
  1052.             return;
  1053.         }
  1054.         
  1055.         
  1056.         viewRect.left = (int) ((**theTE32KHandle).viewRect.left);
  1057.         viewRect.top = (int) ((**theTE32KHandle).viewRect.top);
  1058.         viewRect.right = (int) ((**theTE32KHandle).viewRect.right);
  1059.         viewRect.bottom = (int) ((**theTE32KHandle).viewRect.bottom);
  1060.         
  1061.         GetPort(&oldPort);
  1062.         SetPort((**theTE32KHandle).inPort);
  1063.         
  1064.         if (selStart > selEnd)
  1065.         {
  1066.             firstLine = selStart;
  1067.             selStart = selEnd;
  1068.             selEnd = firstLine;
  1069.         }
  1070.         
  1071.         firstLine = indexToLine(selStart,theTE32KHandle);
  1072.         lastLine = indexToLine(selEnd,theTE32KHandle);
  1073.         
  1074.         
  1075.         TE32KGetPoint(selStart,&selPt,theTE32KHandle);
  1076.         
  1077.         selPt.v -= (**theTE32KHandle).fontAscent;
  1078.         
  1079.         if (selStart <= (**theTE32KHandle).lineStarts[firstLine])
  1080.             selPt.h--;
  1081.         
  1082.         if (selPt.h < -32768L)
  1083.             tempRect1.left = (int) -32768;
  1084.         else if (selPt.h > 32767L)
  1085.             tempRect1.left = (int) 32767;
  1086.         else
  1087.             tempRect1.left = (int) selPt.h;
  1088.         
  1089.         if (selPt.v < -32768L)
  1090.             tempRect1.top = (int) -32768;
  1091.         else if (selPt.v > 32767L)
  1092.             tempRect1.top = (int) 32767;
  1093.         else
  1094.             tempRect1.top = (int) selPt.v;
  1095.         
  1096.         
  1097.         if (firstLine != lastLine)
  1098.         {
  1099.             tempRect1.right = viewRect.right;
  1100.             tempRect1.bottom = tempRect1.top + (**theTE32KHandle).lineHeight;
  1101.         }
  1102.         
  1103.         else
  1104.         {
  1105.             TE32KGetPoint(selEnd,&selPt,theTE32KHandle);
  1106.             
  1107.             selPt.v -= (**theTE32KHandle).fontAscent;
  1108.             selPt.v += (**theTE32KHandle).lineHeight;
  1109.             
  1110.             if (selPt.h < -32768L)
  1111.                 tempRect1.right = (int) -32768;
  1112.             else if (selPt.h > 32767L)
  1113.                 tempRect1.right = (int) 32767;
  1114.             else
  1115.                 tempRect1.right = (int) selPt.h;
  1116.             
  1117.             if (selPt.v < -32768L)
  1118.                 tempRect1.bottom = (int) -32768;
  1119.             else if (selPt.v > 32767L)
  1120.                 tempRect1.bottom = (int) 32767;
  1121.             else
  1122.                 tempRect1.bottom = (int) selPt.v;
  1123.         }
  1124.         
  1125.         if (SectRect(&viewRect,&tempRect1,&theRect))
  1126.             InvertRect(&theRect);
  1127.         
  1128.  
  1129.         
  1130.         if (lastLine > firstLine + 1L)
  1131.         {
  1132.             TE32KGetPoint(selEnd,&selPt,theTE32KHandle);
  1133.             
  1134.             tempRect2.left = viewRect.left;
  1135.             tempRect2.top = tempRect1.bottom;
  1136.             tempRect2.right = viewRect.right;
  1137.             
  1138.             selPt.v -= (**theTE32KHandle).fontAscent;
  1139.             
  1140.             if (selPt.v < -32768L)
  1141.                 tempRect2.bottom = (int) -32768;
  1142.             else if (selPt.v > 32767L)
  1143.                 tempRect2.bottom = (int) 32767;
  1144.             else
  1145.                 tempRect2.bottom = (int) selPt.v;
  1146.             
  1147.             selPt.v += (**theTE32KHandle).fontAscent;
  1148.             
  1149.             if (SectRect(&viewRect,&tempRect2,&theRect))
  1150.                 InvertRect(&theRect);
  1151.         }
  1152.         
  1153.         if (lastLine > firstLine && selEnd > (**theTE32KHandle).lineStarts[lastLine])
  1154.         {
  1155.             if (lastLine == firstLine + 1L)
  1156.             {
  1157.                 TE32KGetPoint(selEnd,&selPt,theTE32KHandle);
  1158.             }
  1159.             
  1160.             selPt.v -= (**theTE32KHandle).fontAscent;
  1161.             
  1162.             if (selPt.v < -32768L)
  1163.                 tempRect3.top = (int) -32768;
  1164.             else if (selPt.v > 32767L)
  1165.                 tempRect3.top = (int) 32767;
  1166.             else
  1167.                 tempRect3.top = (int) selPt.v;
  1168.             
  1169.             selPt.v += (**theTE32KHandle).lineHeight;
  1170.             
  1171.             if (selPt.v < -32768L)
  1172.                 tempRect3.bottom = (int) -32768;
  1173.             else if (selPt.v > 32767L)
  1174.                 tempRect3.bottom = (int) 32767;
  1175.             else
  1176.                 tempRect3.bottom = (int) selPt.v;
  1177.             
  1178.             
  1179.             tempRect3.left = viewRect.left;
  1180.             
  1181.             if (selPt.h < -32768L)
  1182.                 tempRect3.right = (int) -32768;
  1183.             else if (selPt.h > 32767L)
  1184.                 tempRect3.right = (int) 32767;
  1185.             else
  1186.                 tempRect3.right = (int) selPt.h;
  1187.             
  1188.             
  1189.             if (SectRect(&viewRect,&tempRect3,&theRect))
  1190.                 InvertRect(&theRect);
  1191.         }
  1192.         
  1193.         
  1194.         SetPort(oldPort);
  1195.     }
  1196. }
  1197.  
  1198.  
  1199.  
  1200.  
  1201.  
  1202. void    TE32KClick(Point startPoint,unsigned char extend,TE32KHandle theTE32KHandle)
  1203. {
  1204. register long                selIndex,selAnchor,selLast,teLength;
  1205. register unsigned char        *textPtr;
  1206. LongPoint                    selPt,tempPt;
  1207. Point                        mousePt;
  1208. GrafPtr                        oldPort;
  1209. unsigned char                ch;
  1210. long                        oldClickLoc;
  1211.  
  1212.  
  1213.     if (theTE32KHandle && (**theTE32KHandle).active)
  1214.     {
  1215.         clickedTE32KH = theTE32KHandle;
  1216.         
  1217.         teLength = (**theTE32KHandle).teLength;
  1218.         
  1219.         selPt.h = (long) startPoint.h;
  1220.         selPt.v = (long) startPoint.v;
  1221.         
  1222.         selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  1223.         
  1224.         oldClickLoc = (**theTE32KHandle).clickLoc;
  1225.         (**theTE32KHandle).clickLoc = selIndex;
  1226.  
  1227.         if ((**theTE32KHandle).caretState)
  1228.             xorCaret(theTE32KHandle);
  1229.         
  1230.         if (!extend && teLength > 0L && TickCount() < (**theTE32KHandle).clickTime + GetDblTime() && oldClickLoc == selIndex)
  1231.         {
  1232.             if ((**theTE32KHandle).selStart != (**theTE32KHandle).selEnd)
  1233.                 invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1234.             
  1235.             selAnchor = selIndex;
  1236.             selIndex = selAnchor;
  1237.             textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  1238.             
  1239.             while (selIndex > 0L)
  1240.             {
  1241.                 ch = textPtr[selIndex-1L];
  1242.                 
  1243.                 if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))
  1244.                     selIndex--;
  1245.                 else
  1246.                     break;
  1247.             }
  1248.             
  1249.             if (selIndex < 0L)
  1250.                 (**theTE32KHandle).selStart =  0L;
  1251.             else
  1252.                 (**theTE32KHandle).selStart = selIndex;
  1253.             
  1254.             selIndex = selAnchor;
  1255.             teLength = (**theTE32KHandle).teLength;
  1256.             
  1257.             while (selIndex < teLength)
  1258.             {
  1259.                 ch = textPtr[selIndex];
  1260.                 
  1261.                 if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))
  1262.                     selIndex++;
  1263.                 else
  1264.                     break;
  1265.             }
  1266.             
  1267.             if (selIndex > teLength)
  1268.                 (**theTE32KHandle).selEnd =  teLength;
  1269.             else
  1270.                 (**theTE32KHandle).selEnd = selIndex;
  1271.             
  1272.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1273.         }
  1274.         
  1275.         else
  1276.         {
  1277.             if (!extend)
  1278.             {
  1279.                 if ((**theTE32KHandle).selStart != (**theTE32KHandle).selEnd)
  1280.                     invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1281.                 
  1282.                 (**theTE32KHandle).selStart = selIndex;
  1283.                 (**theTE32KHandle).selEnd = selIndex;
  1284.                     
  1285.                 (**theTE32KHandle).clikStuff = FALSE;
  1286.                 TE32KGetPoint(selIndex,&selPt,theTE32KHandle);
  1287.                 
  1288.                 if ((**theTE32KHandle).crOnly)
  1289.                     (**theTE32KHandle).selPoint = selPt;
  1290.                 
  1291.                 else
  1292.                 {
  1293.                     (**theTE32KHandle).clikStuff = TRUE;
  1294.                     TE32KGetPoint(selIndex,&tempPt,theTE32KHandle);
  1295.                     
  1296.                     if ((selPt.h - startPoint.h)*(selPt.h - startPoint.h) + (selPt.v - startPoint.v)*(selPt.v - startPoint.v) <
  1297.                         (tempPt.h - startPoint.h)*(tempPt.h - startPoint.h) + (tempPt.v - startPoint.v)*(tempPt.v - startPoint.v))
  1298.                             (**theTE32KHandle).selPoint = selPt;
  1299.                     else
  1300.                             (**theTE32KHandle).selPoint = tempPt;
  1301.                 }
  1302.                 
  1303.                 xorCaret(theTE32KHandle);
  1304.             }
  1305.                 
  1306.                 
  1307.             
  1308.             if (extend || StillDown())
  1309.             {
  1310.                 if (extend)
  1311.                 {
  1312.                     if (selIndex >= (**theTE32KHandle).selEnd)
  1313.                     {
  1314.                         selAnchor = (**theTE32KHandle).selStart;
  1315.                         selLast = (**theTE32KHandle).selEnd;
  1316.                     }
  1317.                     else
  1318.                     {
  1319.                         selAnchor = (**theTE32KHandle).selEnd;
  1320.                         selLast = (**theTE32KHandle).selStart;
  1321.                     }
  1322.                 }
  1323.                 else
  1324.                 {
  1325.                     selAnchor = selIndex;
  1326.                     selLast = selIndex;
  1327.                 }
  1328.                 
  1329.                 GetPort(&oldPort);
  1330.                 SetPort((**theTE32KHandle).inPort);
  1331.                 
  1332.                 if (extend)
  1333.                     goto DOHILITE;
  1334.                 
  1335.                 while (StillDown())
  1336.                 {
  1337.                     if (selIndex >= selAnchor)
  1338.                     {
  1339.                         (**theTE32KHandle).selStart = selAnchor;
  1340.                         (**theTE32KHandle).selEnd = selIndex;
  1341.                     }
  1342.                     else
  1343.                     {
  1344.                         (**theTE32KHandle).selStart = selIndex;
  1345.                         (**theTE32KHandle).selEnd = selAnchor;
  1346.                     }
  1347.                     
  1348.                     if ((**theTE32KHandle).clikLoop)
  1349.                         (*((**theTE32KHandle).clikLoop)) ();
  1350.                     
  1351.                     GetMouse(&mousePt);
  1352.                     
  1353.                     selPt.h = (long) mousePt.h;
  1354.                     selPt.v = (long) mousePt.v;
  1355.                     
  1356.                     selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  1357.     
  1358.     DOHILITE:
  1359.                     if (selLast >= selAnchor)
  1360.                     {
  1361.                         if (selIndex > selLast)
  1362.                             invertSelRange(selLast,selIndex,theTE32KHandle);
  1363.                         
  1364.                         else if (selIndex>=selAnchor && selIndex<selLast)
  1365.                             invertSelRange(selIndex,selLast,theTE32KHandle);
  1366.                         
  1367.                         else if (selIndex<selAnchor)
  1368.                         {
  1369.                             invertSelRange(selAnchor,selLast,theTE32KHandle);
  1370.                             invertSelRange(selIndex,selAnchor,theTE32KHandle);
  1371.                         }
  1372.                     }
  1373.                     
  1374.                     else
  1375.                     {
  1376.                         if (selIndex < selLast)
  1377.                             invertSelRange(selIndex,selLast,theTE32KHandle);
  1378.                         
  1379.                         else if (selIndex<=selAnchor && selIndex>selLast)
  1380.                             invertSelRange(selLast,selIndex,theTE32KHandle);
  1381.                         
  1382.                         else if (selIndex>selAnchor)
  1383.                         {
  1384.                             invertSelRange(selLast,selAnchor,theTE32KHandle);
  1385.                             invertSelRange(selAnchor,selIndex,theTE32KHandle);
  1386.                         }
  1387.                     }
  1388.                     
  1389.                     selLast = selIndex;
  1390.                 }
  1391.                 
  1392.                 SetPort(oldPort);
  1393.                 
  1394.                 
  1395.                 if (selIndex >= selAnchor)
  1396.                 {
  1397.                     (**theTE32KHandle).selStart = selAnchor;
  1398.                     (**theTE32KHandle).selEnd = selIndex;
  1399.                 }
  1400.                 else
  1401.                 {
  1402.                     (**theTE32KHandle).selStart = selIndex;
  1403.                     (**theTE32KHandle).selEnd = selAnchor;
  1404.                 }
  1405.             }
  1406.         }
  1407.         
  1408.         (**theTE32KHandle).clickTime = TickCount();
  1409.         
  1410.         clickedTE32KH = 0L;
  1411.     }
  1412. }
  1413.  
  1414.  
  1415.  
  1416.  
  1417.  
  1418. void    TE32KSetSelect(long selStart,long selEnd,TE32KHandle theTE32KHandle)
  1419. {
  1420.     if (theTE32KHandle)
  1421.     {
  1422.         if ((**theTE32KHandle).active)
  1423.         {
  1424.             if ((**theTE32KHandle).selStart != (**theTE32KHandle).selEnd)
  1425.                 invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1426.             
  1427.             else if ((**theTE32KHandle).caretState)
  1428.                 xorCaret(theTE32KHandle);
  1429.         }
  1430.                 
  1431.         if (selStart <= selEnd)
  1432.         {
  1433.             (**theTE32KHandle).selStart = selStart;
  1434.             (**theTE32KHandle).selEnd = selEnd;
  1435.         }
  1436.         else
  1437.         {
  1438.             (**theTE32KHandle).selStart = selEnd;
  1439.             (**theTE32KHandle).selEnd = selStart;
  1440.         }
  1441.         
  1442.         if ((**theTE32KHandle).active)
  1443.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1444.     }
  1445. }
  1446.  
  1447.  
  1448.  
  1449.  
  1450.  
  1451. OSErr TE32KToScrap(void)
  1452. {
  1453. OSErr err;
  1454.  
  1455.     if (TE32KScrpHandle && TE32KScrpLength > 0L)
  1456.     {
  1457.         if ((err = ZeroScrap()) != noErr)
  1458.             return(err);
  1459.  
  1460.         HLockHi(TE32KScrpHandle);
  1461.  
  1462.         if ((err = PutScrap(TE32KScrpLength,'TEXT',(Ptr) *TE32KScrpHandle)) != noErr)
  1463.             return(err);
  1464.  
  1465.         HUnlock(TE32KScrpHandle);
  1466.  
  1467.         return(noErr);
  1468.     }
  1469.     
  1470.     return(noScrapErr);
  1471. }
  1472.  
  1473.  
  1474.  
  1475.  
  1476. OSErr TE32KFromScrap(void)
  1477. {
  1478. long offset;
  1479. OSErr err;
  1480.  
  1481.     if (TE32KScrpHandle && (err = GetScrap(0L,'TEXT',&offset)) > 0L)
  1482.     {
  1483.         TE32KScrpLength = GetScrap(TE32KScrpHandle,'TEXT',&offset);
  1484.         
  1485.         if (TE32KScrpLength > 0L)
  1486.             return(noErr);
  1487.         
  1488.         else if (TE32KScrpLength == 0L)
  1489.             return(noTypeErr);
  1490.         
  1491.         else
  1492.             return(TE32KScrpLength);
  1493.     }
  1494.     
  1495.     if (TE32KScrpHandle == NULL)
  1496.         return(noScrapErr);
  1497.     
  1498.     else
  1499.         return(err);
  1500. }
  1501.  
  1502.  
  1503.  
  1504.  
  1505. void    TE32KCopy(TE32KHandle theTE32KHandle)
  1506. {
  1507.     if (theTE32KHandle && TE32KScrpHandle && (**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1508.     {
  1509.         SetHandleSize(TE32KScrpHandle,(**theTE32KHandle).selEnd - (**theTE32KHandle).selStart);
  1510.         
  1511.         if (!MemError() && GetHandleSize(TE32KScrpHandle) >= ((**theTE32KHandle).selEnd - (**theTE32KHandle).selStart))
  1512.         {
  1513.             TE32KScrpLength = (**theTE32KHandle).selEnd - (**theTE32KHandle).selStart;
  1514.             
  1515.             HLock(TE32KScrpHandle);
  1516.             HLock((**theTE32KHandle).hText);
  1517.             
  1518.             BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*TE32KScrpHandle,TE32KScrpLength);
  1519.             
  1520.             HUnlock((**theTE32KHandle).hText);
  1521.             HUnlock(TE32KScrpHandle);
  1522.         }
  1523.     }
  1524. }
  1525.  
  1526.  
  1527.  
  1528.  
  1529.  
  1530. void    TE32KCut(TE32KHandle theTE32KHandle)
  1531. {
  1532.     if (theTE32KHandle && TE32KScrpHandle && (**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1533.     {
  1534.         TE32KCopy(theTE32KHandle);
  1535.         TE32KDelete(theTE32KHandle);
  1536.     }
  1537. }
  1538.  
  1539.  
  1540.  
  1541. void    TE32KDelete(TE32KHandle theTE32KHandle)
  1542. {
  1543. LongRect            updateRect;
  1544. register long        *theLine,*otherLine,theLineStart,i,delta;
  1545. long                firstLine,lastLine;
  1546. Rect                tempRect;
  1547. RgnHandle            updateRgn;
  1548. GrafPtr                oldPort;
  1549.  
  1550.  
  1551.     if (theTE32KHandle && (**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1552.     {
  1553.         invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1554.                 
  1555.         firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  1556.         lastLine = indexToLine((**theTE32KHandle).selEnd,theTE32KHandle);
  1557.         
  1558.         updateRect = (**theTE32KHandle).viewRect;
  1559.         updateRect.top = (**theTE32KHandle).destRect.top + (firstLine + 1L) * (**theTE32KHandle).lineHeight;
  1560.         LongRectToRect(&updateRect,&tempRect);
  1561.         
  1562.         GetPort(&oldPort);
  1563.         SetPort((**theTE32KHandle).inPort);
  1564.                 
  1565.         updateRgn = NewRgn();
  1566.         ScrollRect(&tempRect,0,-(**theTE32KHandle).lineHeight * (lastLine - firstLine),updateRgn);
  1567.         tempRect = (**updateRgn).rgnBBox;
  1568.         DisposeRgn(updateRgn);
  1569.         
  1570.         SetPort(oldPort);
  1571.         
  1572.         if ((**theTE32KHandle).selEnd != (**theTE32KHandle).teLength)
  1573.         {
  1574.             HLock((**theTE32KHandle).hText);
  1575.             BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selEnd,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,(**theTE32KHandle).teLength - (**theTE32KHandle).selEnd);
  1576.             HUnlock((**theTE32KHandle).hText);
  1577.         }
  1578.         
  1579.         delta = (**theTE32KHandle).selEnd - (**theTE32KHandle).selStart;
  1580.         
  1581.         theLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1582.         otherLine = &((**theTE32KHandle).lineStarts[lastLine + 1L]);
  1583.         i = (**theTE32KHandle).nLines - lastLine;
  1584.         
  1585.         while (i--)
  1586.         {
  1587.             theLineStart = *(otherLine++);
  1588.             theLineStart -= delta;
  1589.             *(theLine++) = theLineStart;
  1590.         }
  1591.         
  1592.         (**theTE32KHandle).teLength -= delta;
  1593.         (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1594.         (**theTE32KHandle).nLines -= (lastLine - firstLine);
  1595.         
  1596.         RectToLongRect(&tempRect,&updateRect);
  1597.         TE32KUpdate(&updateRect,theTE32KHandle);    /* update scrolled stuff */
  1598.         
  1599.         updateLine(firstLine,theTE32KHandle,TRUE,0L);
  1600.     }
  1601. }
  1602.  
  1603.  
  1604.  
  1605.  
  1606.  
  1607. void    TE32KInsert(Ptr textPtr,register long textLength,TE32KHandle theTE32KHandle)
  1608. {
  1609. register long                *theLine,*otherLine,i,numCRs;
  1610. long                        firstLine,teLength,maxLineStarts,sizeTE32KHandle;
  1611. register unsigned char        *charPtr,*charBase;
  1612. RgnHandle                    updateRgn;
  1613. Rect                        tempRect;
  1614. GrafPtr                        oldPort;
  1615.  
  1616.  
  1617.     if (theTE32KHandle && textPtr && textLength > 0L)
  1618.     {
  1619.         if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1620.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1621.         
  1622.         if (textLength == 1L)
  1623.         {
  1624.             TE32KKey(*((unsigned char *) textPtr),theTE32KHandle);
  1625.         }
  1626.         
  1627.         else
  1628.         {
  1629.             firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  1630.             
  1631.             teLength = (**theTE32KHandle).teLength + textLength;
  1632.             
  1633.             if (GetHandleSize((**theTE32KHandle).hText) < teLength)
  1634.             {
  1635.                 SetHandleSize((**theTE32KHandle).hText,teLength + EXTRATEXTBUFF);
  1636.                 
  1637.                 if (MemError() || GetHandleSize((**theTE32KHandle).hText) < teLength + EXTRATEXTBUFF)
  1638.                     return;
  1639.             }
  1640.             
  1641.             HLock((**theTE32KHandle).hText);
  1642.             
  1643.             if ((**theTE32KHandle).teLength - (**theTE32KHandle).selStart)
  1644.                 BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart + textLength,(**theTE32KHandle).teLength - (**theTE32KHandle).selStart);
  1645.             
  1646.             BlockMove(textPtr,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,textLength);
  1647.             
  1648.             HUnlock((**theTE32KHandle).hText);
  1649.             
  1650.             i = textLength;
  1651.             numCRs = 0L;
  1652.             charPtr = (unsigned char *) textPtr;
  1653.             
  1654.             while (i--)
  1655.             {
  1656.                 if (*charPtr == '\r' || *charPtr == '\n')
  1657.                     numCRs++;
  1658.                 
  1659.                 charPtr++;
  1660.             }
  1661.             
  1662.             if (numCRs)
  1663.             {
  1664.                 sizeTE32KHandle  = GetHandleSize((Handle) theTE32KHandle);
  1665.                 maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  1666.                 
  1667.                 if ((**theTE32KHandle).nLines + numCRs >= maxLineStarts)
  1668.                 {
  1669.                     sizeTE32KHandle = (long) sizeof(TE32KRec) + (long) sizeof(long)*((**theTE32KHandle).nLines + numCRs + EXTRALINESTARTS);
  1670.                     maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  1671.                     
  1672.                     SetHandleSize((Handle) theTE32KHandle,sizeTE32KHandle);
  1673.                     
  1674.                     if (MemError())
  1675.                         return;
  1676.                 }
  1677.                 
  1678.                 theLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  1679.                 otherLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines + numCRs]);
  1680.                 i = (**theTE32KHandle).nLines - firstLine;
  1681.                 
  1682.                 while (i--)
  1683.                     *(otherLine--) = *(theLine--) + textLength;
  1684.                 
  1685.                 charPtr = (unsigned char *) (*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart) + 1L;
  1686.                 charBase = (unsigned char *) *((**theTE32KHandle).hText);
  1687.                 theLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1688.                 i = numCRs;
  1689.                 
  1690.                 while (i--)
  1691.                 {
  1692.                     while (*charPtr != '\r' && *charPtr != '\n')
  1693.                         charPtr++;
  1694.                     
  1695.                     charPtr++;
  1696.                     
  1697.                     *theLine++ = charPtr - charBase;
  1698.                 }
  1699.                 
  1700.                 LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  1701.                 tempRect.top = (**theTE32KHandle).destRect.top + (firstLine + 1L) * (**theTE32KHandle).lineHeight;
  1702.                 
  1703.                 GetPort(&oldPort);
  1704.                 SetPort((**theTE32KHandle).inPort);
  1705.                 
  1706.                 updateRgn = NewRgn();
  1707.                 ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * numCRs,updateRgn);
  1708.                 DisposeRgn(updateRgn);
  1709.                 
  1710.                 SetPort(oldPort);
  1711.             }
  1712.             
  1713.             else
  1714.             {
  1715.                 theLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1716.                 i = (**theTE32KHandle).nLines - firstLine;
  1717.                 
  1718.                 while (i--)
  1719.                     *(theLine++) += textLength;
  1720.             }
  1721.             
  1722.             
  1723.             (**theTE32KHandle).teLength = teLength;
  1724.             (**theTE32KHandle).selStart += textLength;
  1725.             (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1726.             (**theTE32KHandle).nLines += numCRs;
  1727.             
  1728.             do
  1729.             {
  1730.                 updateLine(firstLine,theTE32KHandle,TRUE,0L);
  1731.                 
  1732.                 if (numCRs)
  1733.                 {
  1734.                     theLine = (**theTE32KHandle).lineStarts;
  1735.                     charPtr = (unsigned char *) *((**theTE32KHandle).hText);
  1736.                     
  1737.                     do
  1738.                     {
  1739.                         firstLine++;
  1740.                         
  1741.                     } while (firstLine < (**theTE32KHandle).nLines && charPtr[theLine[firstLine] - 1L] != '\r' && charPtr[theLine[firstLine] - 1L] != '\n');
  1742.                     
  1743.                 }
  1744.                 
  1745.             } while (numCRs--);
  1746.         }
  1747.     }
  1748. }
  1749.  
  1750.  
  1751.  
  1752.  
  1753.  
  1754.  
  1755. void    TE32KPaste(TE32KHandle theTE32KHandle)
  1756. {
  1757.     if (theTE32KHandle && TE32KScrpHandle && TE32KScrpLength > 0L)
  1758.     {
  1759.         if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1760.             TE32KDelete( theTE32KHandle);
  1761.         
  1762.         HLock(TE32KScrpHandle);
  1763.         
  1764.         TE32KInsert(*TE32KScrpHandle,TE32KScrpLength,theTE32KHandle);
  1765.         
  1766.         HUnlock(TE32KScrpHandle);
  1767.     }
  1768. }
  1769.  
  1770.  
  1771.  
  1772.  
  1773. Handle    TE32KScrapHandle()
  1774. {
  1775.     return(TE32KScrpHandle);
  1776. }
  1777.  
  1778.  
  1779. long    TE32KGetScrapLen()
  1780. {
  1781.     return(TE32KScrpLength);
  1782. }
  1783.  
  1784.  
  1785. void    TE32KSetScrapLen(long newLength)
  1786. {
  1787.     TE32KScrpLength = newLength;
  1788. }
  1789.  
  1790.  
  1791.  
  1792.  
  1793. void    TE32KSelView(TE32KHandle theTE32KHandle)
  1794. {
  1795. register long    deltaV,deltaH,screenLines,lineHeight,viewTop,viewBot,selPtV,ascent;
  1796.  
  1797.     if (theTE32KHandle && (**theTE32KHandle).active)
  1798.     {
  1799.         if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd)
  1800.         {
  1801.             selPtV = (**theTE32KHandle).selPoint.v;
  1802.             viewTop = (**theTE32KHandle).viewRect.top;
  1803.             viewBot = (**theTE32KHandle).viewRect.bottom;
  1804.             lineHeight = (**theTE32KHandle).lineHeight;
  1805.             ascent = (**theTE32KHandle).fontAscent;
  1806.             
  1807.             deltaV = viewTop - (**theTE32KHandle).destRect.top;
  1808.             deltaV = deltaV - (deltaV/lineHeight)*lineHeight;
  1809.             
  1810.             if (selPtV - ascent < viewTop)
  1811.             {
  1812.                 deltaV += viewTop - (selPtV - ascent);
  1813.             }
  1814.             
  1815.             else if (selPtV > viewBot)
  1816.             {
  1817.                 screenLines = (viewBot - viewTop) / lineHeight;
  1818.                 
  1819.                 deltaV -= (selPtV - ascent + lineHeight) - (viewTop + screenLines * lineHeight);
  1820.             }
  1821.             
  1822.             
  1823.             if ((**theTE32KHandle).selPoint.h <= (**theTE32KHandle).viewRect.left)
  1824.             {
  1825.                 deltaH = (**theTE32KHandle).viewRect.left - (**theTE32KHandle).selPoint.h;
  1826.                 deltaH = (2L + deltaH/(**theTE32KHandle).lineHeight) * (**theTE32KHandle).lineHeight;
  1827.                 
  1828.                 if ((**theTE32KHandle).destRect.left + deltaH > (**theTE32KHandle).viewRect.left)
  1829.                     deltaH = (**theTE32KHandle).viewRect.left - (**theTE32KHandle).destRect.left;
  1830.             }
  1831.             
  1832.             else if ((**theTE32KHandle).selPoint.h > (**theTE32KHandle).viewRect.right)
  1833.             {
  1834.                 deltaH = (**theTE32KHandle).selPoint.h - (**theTE32KHandle).viewRect.right;
  1835.                 deltaH = -(2L + deltaH/(**theTE32KHandle).lineHeight) * (**theTE32KHandle).lineHeight;
  1836.             }
  1837.             
  1838.             else
  1839.                 deltaH = 0L;
  1840.             
  1841.             if (deltaV || deltaH)
  1842.                 TE32KScroll(deltaH,deltaV,theTE32KHandle);
  1843.         }
  1844.     }
  1845. }
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851. static    DoDeleteKey(TE32KHandle theTE32KHandle)
  1852. {
  1853. Rect            tempRect;
  1854. RgnHandle        updateRgn;
  1855. int                chWidth;
  1856. long            firstLine;
  1857. register int    *theCharWidths;
  1858. register long    i,*lineStarts,selIndex,*otherLine;
  1859. LongRect        updateRect;
  1860. LongPoint        selPt;
  1861. unsigned char    ch,prevChar;
  1862. GrafPtr            oldPort;
  1863.  
  1864.     if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1865.         TE32KDelete(theTE32KHandle);
  1866.     
  1867.     else if ((**theTE32KHandle).selStart > 0L)
  1868.     {
  1869.         ch = ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart - 1L];
  1870.         
  1871.         if ((**theTE32KHandle).selStart >= 2L)
  1872.             prevChar = ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart - 2L];
  1873.         else
  1874.             prevChar = '\0';
  1875.         
  1876.         firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  1877.         
  1878.         HLock((**theTE32KHandle).hText);
  1879.         BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart - 1L,(**theTE32KHandle).teLength - (**theTE32KHandle).selEnd);
  1880.         HUnlock((**theTE32KHandle).hText);
  1881.         
  1882.         (**theTE32KHandle).teLength--;
  1883.         (**theTE32KHandle).selStart--;
  1884.         (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1885.         
  1886.         if ((ch == '\r' || ch == '\n') && ((**theTE32KHandle).crOnly || (**theTE32KHandle).teLength == (**theTE32KHandle).selStart || prevChar == '\r' || prevChar == '\n'))
  1887.         {
  1888.             lineStarts = &((**theTE32KHandle).lineStarts[firstLine]);
  1889.             otherLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1890.             
  1891.             i = (**theTE32KHandle).nLines - firstLine;
  1892.             
  1893.             while (i--)
  1894.             {
  1895.                 selIndex = *(otherLine++);
  1896.                 *(lineStarts++) = --selIndex;
  1897.             }
  1898.             
  1899.             (**theTE32KHandle).nLines--;
  1900.             
  1901.             if (firstLine > 0L)
  1902.                 firstLine--;
  1903.             
  1904.             updateRect = (**theTE32KHandle).viewRect;
  1905.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  1906.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  1907.             TE32KUpdate(&updateRect,theTE32KHandle);
  1908.             
  1909.             LongRectToRect(&updateRect,&tempRect);
  1910.             tempRect.top = tempRect.bottom;
  1911.             
  1912.             if ((**theTE32KHandle).viewRect.bottom < -32768L)
  1913.                 tempRect.bottom = -32768;
  1914.             else if ((**theTE32KHandle).viewRect.bottom > 32767L)
  1915.                 tempRect.bottom = 32767;
  1916.             else
  1917.                 tempRect.bottom = (int) (**theTE32KHandle).viewRect.bottom;
  1918.             
  1919.             
  1920.             GetPort(&oldPort);
  1921.             SetPort((**theTE32KHandle).inPort);
  1922.         
  1923.             updateRgn = NewRgn();
  1924.             ScrollRect(&tempRect,0,-(**theTE32KHandle).lineHeight,updateRgn);
  1925.             tempRect = (**updateRgn).rgnBBox;
  1926.             DisposeRgn(updateRgn);
  1927.             
  1928.             SetPort(oldPort);
  1929.             
  1930.             TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  1931.             (**theTE32KHandle).selPoint = selPt;
  1932.             
  1933.             RectToLongRect(&tempRect,&updateRect);
  1934.             
  1935.             if ((**theTE32KHandle).caretState)
  1936.                 xorCaret(theTE32KHandle);
  1937.         
  1938.             TE32KUpdate(&updateRect,theTE32KHandle);
  1939.         }
  1940.         
  1941.         else
  1942.         {
  1943.             lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  1944.             i = (**theTE32KHandle).nLines - firstLine;
  1945.             
  1946.             if (ch == '\r' || ch == '\n')
  1947.                 i++;
  1948.             
  1949.             
  1950.             while (i--)
  1951.                 (*(lineStarts--))--;
  1952.             
  1953.             theCharWidths = (**theTE32KHandle).theCharWidths;
  1954.             
  1955.             if (ch == TAB)
  1956.                 chWidth = (**theTE32KHandle).tabWidth;
  1957.             else
  1958.                 chWidth = theCharWidths[ch];
  1959.             
  1960.             if (ch == '\r' || ch == '\n')
  1961.             {
  1962.                 firstLine--;
  1963.                 
  1964.                 updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  1965.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  1966.                 updateRect.left = (**theTE32KHandle).viewRect.left;
  1967.                 updateRect.right = (**theTE32KHandle).viewRect.right;
  1968.  
  1969.             }
  1970.             
  1971.             else
  1972.             {
  1973.                 updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  1974.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  1975.                 updateRect.left = (**theTE32KHandle).selPoint.h - chWidth;
  1976.                 updateRect.right = (**theTE32KHandle).viewRect.right;
  1977.             }
  1978.             
  1979.             if ((**theTE32KHandle).caretState)
  1980.                 xorCaret(theTE32KHandle);
  1981.             
  1982.             if ((**theTE32KHandle).crOnly)
  1983.                 TE32KUpdate(&updateRect,theTE32KHandle);
  1984.             
  1985.             else
  1986.             {
  1987.                 updateLine(firstLine,theTE32KHandle,TRUE,&updateRect);
  1988.             }
  1989.         }
  1990.     }
  1991. }
  1992.  
  1993.  
  1994.  
  1995.  
  1996.  
  1997. static    DoArrowKeys(unsigned char ch,TE32KHandle theTE32KHandle)
  1998. {
  1999. LongPoint        selPt,tempPt1,tempPt2;
  2000. long            firstLine,selIndex;
  2001. unsigned char    currentChar;
  2002.  
  2003.     if ((**theTE32KHandle).caretState)
  2004.         xorCaret(theTE32KHandle);
  2005.     
  2006.     if (!shiftKeyDown())
  2007.     {
  2008.         if (ch==LEFTARROW || ch==RIGHTARROW)
  2009.         {
  2010.             if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  2011.                 invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  2012.             
  2013.             if (ch==LEFTARROW && (**theTE32KHandle).selStart > 0L)
  2014.             {
  2015.                 currentChar = ((unsigned char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).selStart - 1L];
  2016.                 
  2017.                 firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2018.                 
  2019.                 (**theTE32KHandle).clikStuff = FALSE;
  2020.                 TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2021.                 
  2022.                 if (!(**theTE32KHandle).crOnly && firstLine > 0 && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine] && currentChar != '\r'  && currentChar != '\n' && (**theTE32KHandle).selPoint.h == selPt.h && (**theTE32KHandle).selPoint.v == selPt.v)
  2023.                 {
  2024.                     (**theTE32KHandle).clikStuff = TRUE;
  2025.                     TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2026.                     
  2027.                     (**theTE32KHandle).selPoint = selPt;
  2028.                     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2029.                     
  2030.                     return;
  2031.                 }
  2032.                 
  2033.                 else
  2034.                 {
  2035.                     (**theTE32KHandle).selStart--;
  2036.                     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2037.                 }
  2038.             }
  2039.             else if (ch==RIGHTARROW && (**theTE32KHandle).selEnd < (**theTE32KHandle).teLength)
  2040.             {
  2041.                 currentChar = ((unsigned char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).selEnd];
  2042.                 
  2043.                 firstLine = indexToLine((**theTE32KHandle).selEnd,theTE32KHandle);
  2044.                 
  2045.                 (**theTE32KHandle).clikStuff = TRUE;
  2046.                 TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2047.                 
  2048.                 if (!(**theTE32KHandle).crOnly && (**theTE32KHandle).selEnd == (**theTE32KHandle).lineStarts[firstLine] && (**theTE32KHandle).selPoint.h == selPt.h && (**theTE32KHandle).selPoint.v == selPt.v)
  2049.                 {
  2050.                     (**theTE32KHandle).clikStuff = FALSE;
  2051.                     TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2052.                     
  2053.                     (**theTE32KHandle).selPoint = selPt;
  2054.                     (**theTE32KHandle).selStart = (**theTE32KHandle).selEnd;
  2055.                     
  2056.                     return;
  2057.                 }
  2058.                 
  2059.                 else if (!(**theTE32KHandle).crOnly && firstLine < (**theTE32KHandle).nLines - 1L && (**theTE32KHandle).selStart + 1L == (**theTE32KHandle).lineStarts[firstLine + 1L] && currentChar != '\r' && currentChar != '\n')
  2060.                 {
  2061.                     (**theTE32KHandle).selEnd++;
  2062.                     
  2063.                     (**theTE32KHandle).clikStuff = TRUE;
  2064.                     TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2065.                     
  2066.                     (**theTE32KHandle).selPoint = selPt;
  2067.                     (**theTE32KHandle).selStart = (**theTE32KHandle).selEnd;
  2068.                     
  2069.                     return;
  2070.                 }
  2071.                 
  2072.                 else
  2073.                 {
  2074.                     (**theTE32KHandle).selEnd++;
  2075.                     (**theTE32KHandle).selStart = (**theTE32KHandle).selEnd;
  2076.                 }
  2077.             }
  2078.             
  2079.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  2080.         }
  2081.         
  2082.         else if (ch==UPARROW || ch==DOWNARROW)
  2083.         {
  2084.             if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  2085.             {
  2086.                 invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  2087.                 
  2088.                 if (ch == DOWNARROW)
  2089.                     TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2090.                 else
  2091.                     TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2092.                 
  2093.                 (**theTE32KHandle).selPoint = selPt;
  2094.             }
  2095.                 
  2096.                 
  2097.             if (ch==UPARROW)
  2098.             {
  2099.                 selPt = (**theTE32KHandle).selPoint;
  2100.                 
  2101.                 firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2102.                 
  2103.                 if (firstLine > 0L)
  2104.                 {
  2105.                     selPt.v -= (**theTE32KHandle).lineHeight;
  2106.                     (**theTE32KHandle).selStart = TE32KGetOffset(&selPt,theTE32KHandle);
  2107.                     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2108.                     firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2109.                     
  2110.                     if (!(**theTE32KHandle).crOnly && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine])
  2111.                     {
  2112.                         (**theTE32KHandle).clikStuff = FALSE;
  2113.                         TE32KGetPoint((**theTE32KHandle).selStart,&tempPt1,theTE32KHandle);
  2114.                         
  2115.                         (**theTE32KHandle).clikStuff = TRUE;
  2116.                         TE32KGetPoint((**theTE32KHandle).selStart,&tempPt2,theTE32KHandle);
  2117.                         
  2118.                         if ((selPt.h - tempPt1.h)*(selPt.h - tempPt1.h) + (selPt.v - tempPt1.v)*(selPt.v - tempPt1.v) <
  2119.                             (selPt.h - tempPt2.h)*(selPt.h - tempPt2.h) + (selPt.v - tempPt2.v)*(selPt.v - tempPt2.v))
  2120.                                 (**theTE32KHandle).selPoint = tempPt1;
  2121.                         else
  2122.                                 (**theTE32KHandle).selPoint = tempPt2;
  2123.                         
  2124.                         return;
  2125.                     }
  2126.                     
  2127.                     else
  2128.                     {
  2129.                         (**theTE32KHandle).clikStuff = FALSE;
  2130.                         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2131.                         (**theTE32KHandle).selPoint = selPt;
  2132.                     }
  2133.                 }
  2134.             }
  2135.             
  2136.             else if (ch == DOWNARROW)
  2137.             {
  2138.                 selPt = (**theTE32KHandle).selPoint;
  2139.                 
  2140.                 firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2141.                 
  2142.                 if (firstLine < (**theTE32KHandle).nLines)
  2143.                 {
  2144.                     selPt.v += (**theTE32KHandle).lineHeight;
  2145.                     (**theTE32KHandle).selEnd = TE32KGetOffset(&selPt,theTE32KHandle);
  2146.                     (**theTE32KHandle).selStart = (**theTE32KHandle).selEnd;
  2147.                     firstLine = indexToLine((**theTE32KHandle).selEnd,theTE32KHandle);
  2148.                     
  2149.                     if (!(**theTE32KHandle).crOnly && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine])
  2150.                     {
  2151.                         (**theTE32KHandle).clikStuff = FALSE;
  2152.                         TE32KGetPoint((**theTE32KHandle).selStart,&tempPt1,theTE32KHandle);
  2153.                         
  2154.                         (**theTE32KHandle).clikStuff = TRUE;
  2155.                         TE32KGetPoint((**theTE32KHandle).selStart,&tempPt2,theTE32KHandle);
  2156.                         
  2157.                         if ((selPt.h - tempPt1.h)*(selPt.h - tempPt1.h) + (selPt.v - tempPt1.v)*(selPt.v - tempPt1.v) <
  2158.                             (selPt.h - tempPt2.h)*(selPt.h - tempPt2.h) + (selPt.v - tempPt2.v)*(selPt.v - tempPt2.v))
  2159.                                 (**theTE32KHandle).selPoint = tempPt1;
  2160.                         else
  2161.                                 (**theTE32KHandle).selPoint = tempPt2;
  2162.                         
  2163.                         return;
  2164.                     }
  2165.                     
  2166.                     else
  2167.                     {
  2168.                         (**theTE32KHandle).clikStuff = FALSE;
  2169.                         TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2170.                         (**theTE32KHandle).selPoint = selPt;
  2171.                     }
  2172.                 }
  2173.             }
  2174.             
  2175.             (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2176.             
  2177.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  2178.         }
  2179.     }
  2180.     
  2181.     else
  2182.     {
  2183.         if (ch==LEFTARROW)
  2184.         {
  2185.             if ((**theTE32KHandle).selStart > 0L)
  2186.             {
  2187.                 invertSelRange((**theTE32KHandle).selStart - 1L,(**theTE32KHandle).selStart,theTE32KHandle);
  2188.                 (**theTE32KHandle).selStart--;
  2189.             }
  2190.         }
  2191.         
  2192.         else if (ch==RIGHTARROW)
  2193.         {
  2194.             if ((**theTE32KHandle).selEnd < (**theTE32KHandle).teLength)
  2195.             {
  2196.                 invertSelRange((**theTE32KHandle).selEnd,(**theTE32KHandle).selEnd + 1L,theTE32KHandle);
  2197.                 (**theTE32KHandle).selEnd++;
  2198.             }
  2199.         }
  2200.         
  2201.         else if (ch==UPARROW)
  2202.         {
  2203.             firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2204.             
  2205.             if (firstLine > 0L)
  2206.             {
  2207.                 TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2208.                 selPt.v -= (**theTE32KHandle).lineHeight;
  2209.                 selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  2210.                 
  2211.                 invertSelRange(selIndex,(**theTE32KHandle).selStart,theTE32KHandle);
  2212.                 
  2213.                 (**theTE32KHandle).selStart = selIndex;
  2214.             }
  2215.         }
  2216.         
  2217.         else if (ch==DOWNARROW)
  2218.         {
  2219.             firstLine = indexToLine((**theTE32KHandle).selEnd,theTE32KHandle);
  2220.             
  2221.             if (firstLine < (**theTE32KHandle).nLines - 1L)
  2222.             {
  2223.                 TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2224.                 selPt.v += (**theTE32KHandle).lineHeight;
  2225.                 selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  2226.                 
  2227.                 invertSelRange((**theTE32KHandle).selEnd,selIndex,theTE32KHandle);
  2228.                 
  2229.                 (**theTE32KHandle).selEnd = selIndex;
  2230.             }
  2231.         }
  2232.     }
  2233. }
  2234.  
  2235.  
  2236.  
  2237.  
  2238.  
  2239. static    DoNormalChar(unsigned char ch,TE32KHandle theTE32KHandle)
  2240. {
  2241. Rect            tempRect;
  2242. RgnHandle        updateRgn;
  2243. int                chWidth,destLeftSide;
  2244. register int    *theCharWidths;
  2245. long            teLength,firstLine;
  2246. register long    i,*lineStarts,delta;
  2247. LongPoint        selPt;
  2248. unsigned char    prevChar;
  2249. GrafPtr            oldPort;
  2250. int                oldFont,oldFace,oldSize,oldMode;
  2251.     
  2252.     teLength = (**theTE32KHandle).teLength + 1L;
  2253.     
  2254.     if (GetHandleSize((**theTE32KHandle).hText) < teLength)
  2255.     {
  2256.         SetHandleSize((**theTE32KHandle).hText,teLength + EXTRATEXTBUFF);
  2257.         
  2258.         if (MemError() || GetHandleSize((**theTE32KHandle).hText) < teLength)
  2259.             return;
  2260.     }
  2261.     
  2262.     if ((**theTE32KHandle).caretState)
  2263.         xorCaret(theTE32KHandle);
  2264.     
  2265.     selPt = (**theTE32KHandle).selPoint;
  2266.     
  2267.     selPt.h--;
  2268.     
  2269.     firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2270.     if ((**theTE32KHandle).selStart > 0L)
  2271.         prevChar = ((unsigned char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).lineStarts[firstLine] - 1L];
  2272.     else
  2273.         prevChar = '\r';
  2274.     
  2275.     if ((**theTE32KHandle).crOnly || prevChar =='\r'  || prevChar =='\n' || !(ch == ' ' && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine]))
  2276.     {
  2277.         if (selPt.h < -32768L)
  2278.             tempRect.left = -32768;
  2279.         else if (selPt.h > 32767L)
  2280.             tempRect.left = 32767;
  2281.         else
  2282.             tempRect.left = (int) selPt.h;
  2283.         
  2284.         if ((**theTE32KHandle).viewRect.right < -32768L)
  2285.             tempRect.right = -32768;
  2286.         else if ((**theTE32KHandle).viewRect.right > 32767L)
  2287.             tempRect.right = 32767;
  2288.         else
  2289.             tempRect.right = (int) (**theTE32KHandle).viewRect.right;
  2290.         
  2291.         selPt.v -= (**theTE32KHandle).fontAscent;
  2292.         
  2293.         if (selPt.v < -32768L)
  2294.             tempRect.top = -32768;
  2295.         else if (selPt.v > 32767L)
  2296.             tempRect.top = 32767;
  2297.         else
  2298.             tempRect.top = (int) selPt.v;
  2299.         
  2300.         tempRect.bottom = tempRect.top + (**theTE32KHandle).lineHeight;
  2301.         
  2302.         GetPort(&oldPort);
  2303.         SetPort((**theTE32KHandle).inPort);
  2304.         
  2305.         oldFont = ((**theTE32KHandle).inPort)->txFont;
  2306.         oldFace = ((**theTE32KHandle).inPort)->txFace;
  2307.         oldSize = ((**theTE32KHandle).inPort)->txSize;
  2308.         oldMode = ((**theTE32KHandle).inPort)->txMode;
  2309.         
  2310.         TextFont((**theTE32KHandle).txFont);
  2311.         TextFace((**theTE32KHandle).txFace);
  2312.         TextSize((**theTE32KHandle).txSize);
  2313.         TextMode((**theTE32KHandle).txMode);
  2314.         
  2315.         theCharWidths = (**theTE32KHandle).theCharWidths;
  2316.         
  2317.         if (ch == TAB)
  2318.         {
  2319.             destLeftSide = (**theTE32KHandle).destRect.left + 1L;
  2320.             delta = (**theTE32KHandle).tabWidth;
  2321.             chWidth = (destLeftSide + ((tempRect.left + 1 - destLeftSide + delta)/delta)*delta) - (tempRect.left + 1);
  2322.         }
  2323.         else
  2324.             chWidth = theCharWidths[ch];
  2325.         
  2326.         if (tempRect.left < tempRect.right)
  2327.         {    
  2328.             updateRgn = NewRgn();
  2329.             ScrollRect(&tempRect,chWidth,0,updateRgn);
  2330.             
  2331.             if (tempRect.left+1 + chWidth > tempRect.right)
  2332.                 ClipRect(&tempRect);
  2333.             
  2334.             MoveTo(tempRect.left+1,tempRect.top + (**theTE32KHandle).fontAscent);
  2335.             if (ch != TAB)
  2336.                 DrawChar(ch);
  2337.             
  2338.             if (tempRect.left+1 + chWidth > tempRect.right)
  2339.             {
  2340.                 tempRect.left = -32768;
  2341.                 tempRect.top = -32768;
  2342.                 tempRect.right = 32767;
  2343.                 tempRect.bottom = 32767;
  2344.                 ClipRect(&tempRect);
  2345.             }
  2346.             
  2347.             DisposeRgn(updateRgn);
  2348.         }
  2349.         
  2350.         TextFont(oldFont);
  2351.         TextFace(oldFace);
  2352.         TextSize(oldSize);
  2353.         TextMode(oldMode);
  2354.         
  2355.         SetPort(oldPort);
  2356.     }
  2357.     
  2358.     HLock((**theTE32KHandle).hText);
  2359.     BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart + 1L,(**theTE32KHandle).teLength - (**theTE32KHandle).selStart);
  2360.     HUnlock((**theTE32KHandle).hText);
  2361.     
  2362.     ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart] = ch;
  2363.     
  2364.     lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  2365.     i = (**theTE32KHandle).nLines - firstLine;
  2366.     
  2367.     if (!(**theTE32KHandle).crOnly && prevChar != '\r'  && prevChar != '\n' && ch == ' ' && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine])
  2368.         i++;
  2369.     
  2370.     while (i--)
  2371.         (*(lineStarts--))++;
  2372.     
  2373.     
  2374.     (**theTE32KHandle).teLength++;
  2375.     (**theTE32KHandle).selStart++;
  2376.     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2377.     (**theTE32KHandle).selPoint.h += (long) chWidth;
  2378.     
  2379.     if (!(**theTE32KHandle).crOnly)
  2380.         updateLine(firstLine,theTE32KHandle,FALSE,0L);
  2381.     
  2382.     xorCaret(theTE32KHandle);
  2383. }
  2384.  
  2385.  
  2386.  
  2387.  
  2388.  
  2389. static    DoReturnChar(TE32KHandle theTE32KHandle)
  2390. {
  2391. Rect            tempRect;
  2392. RgnHandle        updateRgn;
  2393. long            teLength,firstLine,lastLine,deltaLines,numAffected,tempFirstLine;
  2394. register long    i,*lineStarts,selIndex,*otherLine;
  2395. LongPoint        selPt;
  2396. LongRect        updateRect;
  2397. unsigned char    prevChar,doWrap;
  2398. GrafPtr            oldPort;
  2399.  
  2400.     teLength = GetHandleSize((Handle) theTE32KHandle);
  2401.     lastLine  = (teLength - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  2402.     
  2403.     if ((**theTE32KHandle).nLines + 1L >= lastLine)
  2404.     {
  2405.         teLength = (long) sizeof(TE32KRec) + (long) sizeof(long)*((**theTE32KHandle).nLines + 1L + EXTRALINESTARTS);
  2406.         
  2407.         SetHandleSize((Handle) theTE32KHandle,teLength);
  2408.         
  2409.         if (MemError()  || GetHandleSize(theTE32KHandle) < teLength)
  2410.             return;
  2411.     }
  2412.     
  2413.     
  2414.     teLength = (**theTE32KHandle).teLength + 1L;
  2415.     
  2416.     if (GetHandleSize((**theTE32KHandle).hText) < teLength)
  2417.     {
  2418.         SetHandleSize((**theTE32KHandle).hText,teLength + EXTRATEXTBUFF);
  2419.         
  2420.         if (MemError() || GetHandleSize((**theTE32KHandle).hText) < teLength)
  2421.             return;
  2422.     }
  2423.     
  2424.     
  2425.     
  2426.     if ((**theTE32KHandle).selStart > 0L)
  2427.         prevChar = ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart - 1L];
  2428.     else
  2429.         prevChar = '\r';
  2430.     
  2431.     if ((**theTE32KHandle).caretState)
  2432.         xorCaret(theTE32KHandle);
  2433.     
  2434.     HLock((**theTE32KHandle).hText);
  2435.     BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart + 1L,(**theTE32KHandle).teLength - (**theTE32KHandle).selStart);
  2436.     HUnlock((**theTE32KHandle).hText);
  2437.     
  2438.     ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart] = RETURN;
  2439.     
  2440.     firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2441.     
  2442.     lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  2443.     otherLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines + 1L]);
  2444.     i = (**theTE32KHandle).nLines - firstLine;
  2445.         
  2446.     while (i--)
  2447.     {
  2448.         selIndex = *(lineStarts--);
  2449.         *(otherLine--) = ++selIndex;
  2450.     
  2451.     }
  2452.     
  2453.     (**theTE32KHandle).lineStarts[firstLine + 1L] = (**theTE32KHandle).selStart + 1L;
  2454.     
  2455.     (**theTE32KHandle).nLines++;
  2456.     (**theTE32KHandle).teLength++;
  2457.     (**theTE32KHandle).selStart++;
  2458.     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2459.     
  2460.     LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2461.     
  2462.     selPt = (**theTE32KHandle).selPoint;
  2463.     selPt.v -= (**theTE32KHandle).fontAscent;
  2464.     selPt.v += (**theTE32KHandle).lineHeight;
  2465.     
  2466.     if (selPt.v < -32768L)
  2467.         tempRect.top = -32768;
  2468.     else if (selPt.v > 32767L)
  2469.         tempRect.top = 32767;
  2470.     else
  2471.         tempRect.top = (int) selPt.v;
  2472.     
  2473.     GetPort(&oldPort);
  2474.     SetPort((**theTE32KHandle).inPort);
  2475.             
  2476.     updateRgn = NewRgn();
  2477.     ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight,updateRgn);
  2478.     DisposeRgn(updateRgn);
  2479.     
  2480.     SetPort(oldPort);
  2481.     
  2482.     if (!(**theTE32KHandle).crOnly)
  2483.     {
  2484.         doWrap = FALSE;
  2485.         tempFirstLine = firstLine;
  2486.         
  2487.         if (tempFirstLine > 0L && LineEndIndex(tempFirstLine - 1L,theTE32KHandle) != (**theTE32KHandle).lineStarts[tempFirstLine])
  2488.         {
  2489.             doWrap = TRUE;
  2490.             tempFirstLine--;
  2491.         }
  2492.         
  2493.         else if (LineEndIndex(tempFirstLine,theTE32KHandle) != (**theTE32KHandle).lineStarts[tempFirstLine + 1L])
  2494.             doWrap = TRUE;
  2495.         
  2496.         
  2497.         if (doWrap)
  2498.         {
  2499.             CalParagraph(tempFirstLine,theTE32KHandle,&deltaLines,&numAffected);
  2500.             
  2501.             if (deltaLines == 0L)
  2502.             {
  2503.                 updateRect = (**theTE32KHandle).viewRect;
  2504.                 updateRect.top = (**theTE32KHandle).destRect.top + tempFirstLine * (**theTE32KHandle).lineHeight;
  2505.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2506.             }
  2507.             
  2508.             else if (deltaLines > 0L)
  2509.             {
  2510.                 firstLine += deltaLines;
  2511.                 
  2512.                 LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2513.                 tempRect.top = (**theTE32KHandle).destRect.top + (tempFirstLine + numAffected - deltaLines) * (**theTE32KHandle).lineHeight;
  2514.                 
  2515.                 GetPort(&oldPort);
  2516.                 SetPort((**theTE32KHandle).inPort);
  2517.                 
  2518.                 updateRgn = NewRgn();
  2519.                 ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2520.                 DisposeRgn(updateRgn);
  2521.                 
  2522.                 SetPort(oldPort);
  2523.                 
  2524.                 updateRect = (**theTE32KHandle).viewRect;
  2525.                 updateRect.top = (**theTE32KHandle).destRect.top + tempFirstLine * (**theTE32KHandle).lineHeight;
  2526.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2527.             }
  2528.             
  2529.             else
  2530.             {
  2531.                 firstLine += deltaLines;
  2532.                 
  2533.                 LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2534.                 tempRect.top = (**theTE32KHandle).destRect.top + (tempFirstLine - 1L + numAffected) * (**theTE32KHandle).lineHeight;
  2535.                 
  2536.                 GetPort(&oldPort);
  2537.                 SetPort((**theTE32KHandle).inPort);
  2538.     
  2539.                 updateRgn = NewRgn();
  2540.                 ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2541.                 DisposeRgn(updateRgn);
  2542.                 
  2543.                 SetPort(oldPort);
  2544.                 
  2545.                 updateRect = (**theTE32KHandle).viewRect;
  2546.                 updateRect.top = (**theTE32KHandle).destRect.top + tempFirstLine * (**theTE32KHandle).lineHeight;
  2547.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2548.             }
  2549.             
  2550.             TE32KUpdate(&updateRect,theTE32KHandle);
  2551.         }
  2552.         
  2553.         
  2554.         firstLine++;
  2555.         
  2556.         CalParagraph(firstLine,theTE32KHandle,&deltaLines,&numAffected);
  2557.         
  2558.         if (deltaLines > 0L)
  2559.         {
  2560.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2561.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine + numAffected - deltaLines) * (**theTE32KHandle).lineHeight;
  2562.             
  2563.             GetPort(&oldPort);
  2564.             SetPort((**theTE32KHandle).inPort);
  2565.     
  2566.             updateRgn = NewRgn();
  2567.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2568.             DisposeRgn(updateRgn);
  2569.             
  2570.             SetPort(oldPort);
  2571.         }
  2572.         
  2573.         else if (deltaLines < 0L)
  2574.         {
  2575.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2576.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine - 1L + numAffected) * (**theTE32KHandle).lineHeight;
  2577.             
  2578.             GetPort(&oldPort);
  2579.             SetPort((**theTE32KHandle).inPort);
  2580.     
  2581.             updateRgn = NewRgn();
  2582.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2583.             DisposeRgn(updateRgn);
  2584.             
  2585.             SetPort(oldPort);
  2586.         }
  2587.         
  2588.         updateRect = (**theTE32KHandle).viewRect;
  2589.         updateRect.top = (**theTE32KHandle).destRect.top + (firstLine - 1L) * (**theTE32KHandle).lineHeight;
  2590.         updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * (numAffected + 1L);
  2591.         
  2592.         if ((**theTE32KHandle).caretState)
  2593.             xorCaret(theTE32KHandle);
  2594.         
  2595.         TE32KUpdate(&updateRect,theTE32KHandle);
  2596.         
  2597.         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2598.         (**theTE32KHandle).selPoint = selPt;
  2599.     }
  2600.     
  2601.     else
  2602.     {
  2603.         updateRect = (**theTE32KHandle).viewRect;
  2604.         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2605.         (**theTE32KHandle).selPoint = selPt;
  2606.         
  2607.         if ((**theTE32KHandle).nLines - firstLine >= 2L && (**theTE32KHandle).lineStarts[firstLine+1L]+1L < (**theTE32KHandle).lineStarts[firstLine + 2L])
  2608.         {
  2609.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2610.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight + (**theTE32KHandle).lineHeight;
  2611.             TE32KUpdate(&updateRect,theTE32KHandle);
  2612.         }
  2613.         else
  2614.         {
  2615.             TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2616.             (**theTE32KHandle).selPoint = selPt;
  2617.             xorCaret(theTE32KHandle);
  2618.         }
  2619.     }
  2620. }
  2621.  
  2622.  
  2623.  
  2624. static    OverTypeSelection(unsigned char ch,TE32KHandle theTE32KHandle)
  2625. {
  2626.     TE32KDelete(theTE32KHandle);
  2627.     
  2628.     if (ch==RETURN)
  2629.         DoReturnChar(theTE32KHandle);
  2630.     
  2631.     else if (ch==TAB || ch >= (unsigned char) 0x20)
  2632.         DoNormalChar(ch,theTE32KHandle);
  2633. }
  2634.  
  2635.  
  2636.  
  2637.  
  2638.  
  2639.  
  2640.  
  2641.  
  2642.  
  2643. void    TE32KKey(unsigned char ch,TE32KHandle theTE32KHandle)
  2644. {
  2645.     if (theTE32KHandle && (**theTE32KHandle).active)
  2646.     {
  2647.         ObscureCursor();
  2648.         
  2649.         if (ch == ENTER)
  2650.             ch = RETURN;
  2651.             
  2652.         
  2653.         if (ch == DELETE)
  2654.             DoDeleteKey(theTE32KHandle);
  2655.         
  2656.         else if (ch==LEFTARROW || ch==RIGHTARROW || ch==UPARROW || ch==DOWNARROW)
  2657.             DoArrowKeys(ch,theTE32KHandle);
  2658.         
  2659.         else if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd && (ch >= 0x20 || ch==TAB || ch==RETURN))
  2660.             OverTypeSelection(ch,theTE32KHandle);
  2661.         
  2662.         else if (ch==TAB || ch >= (unsigned char) 0x20)
  2663.             DoNormalChar(ch,theTE32KHandle);
  2664.         
  2665.         else if (ch==RETURN)
  2666.             DoReturnChar(theTE32KHandle);
  2667.         
  2668.         if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd && !(**theTE32KHandle).caretState)
  2669.             xorCaret(theTE32KHandle);
  2670.     }
  2671. }
  2672.  
  2673.  
  2674.  
  2675.  
  2676.  
  2677. static long paraLines(long firstLine, TE32KHandle theTE32KHandle)
  2678. {
  2679. long                    lastLine,nLines;
  2680. register unsigned char    *charBase;
  2681. register long            *lineStarts;
  2682.  
  2683.     if ((**theTE32KHandle).crOnly)
  2684.         return(1L);
  2685.     
  2686.     lastLine = firstLine + 1L;
  2687.     nLines = (**theTE32KHandle).nLines;
  2688.     charBase = (unsigned char    *) *((**theTE32KHandle).hText);
  2689.     lineStarts = &((**theTE32KHandle).lineStarts[lastLine]);
  2690.     
  2691.     while (lastLine < nLines && charBase[*lineStarts - 1L] != '\r' && charBase[*lineStarts - 1L] != '\n')
  2692.     {
  2693.         lastLine++;
  2694.         lineStarts++;
  2695.     }
  2696.     
  2697.     return(lastLine - firstLine);
  2698. }
  2699.  
  2700.  
  2701.  
  2702.  
  2703.  
  2704. static long    LineEndIndex(long firstLine,TE32KHandle theTE32KHandle)
  2705. {
  2706. register unsigned char    *charPtr;
  2707. register long            charCount;
  2708. register int            *theCharWidths,crOnly,maxLineWidth,lineWidth;
  2709. register unsigned char    ch;
  2710. unsigned char            *charBase;
  2711. Point                    cursorPt;
  2712. int                        rightSide,destLeftSide,tabWidth;
  2713. int                        lineStatus;
  2714. unsigned char            *oldCharPtr;
  2715. long                    maxRewind;
  2716.     
  2717.     if ((**theTE32KHandle).crOnly)
  2718.         return((**theTE32KHandle).lineStarts[firstLine + 1L]);
  2719.     
  2720.     maxLineWidth = (**theTE32KHandle).maxLineWidth;
  2721.     crOnly = (**theTE32KHandle).crOnly;
  2722.     
  2723.     charBase = (unsigned char *) *((**theTE32KHandle).hText);
  2724.     charPtr = charBase + (**theTE32KHandle).lineStarts[firstLine];
  2725.     charCount = (**theTE32KHandle).teLength - (**theTE32KHandle).lineStarts[firstLine];
  2726.     
  2727.     if (charCount > (**theTE32KHandle).teLength)
  2728.         charCount = (**theTE32KHandle).teLength;
  2729.     
  2730.     lineStatus = 0;
  2731.     lineWidth = 0;
  2732.     
  2733.     if (charCount)
  2734.     {
  2735.         rightSide = (int) ((**theTE32KHandle).destRect.right);
  2736.         destLeftSide = (int) ((**theTE32KHandle).destRect.left + 1L);
  2737.         cursorPt.h = destLeftSide;
  2738.         tabWidth = (long) (**theTE32KHandle).tabWidth;
  2739.         
  2740.         theCharWidths = (**theTE32KHandle).theCharWidths;
  2741.         
  2742.         ch = ' ';
  2743.         
  2744.         while (charCount-- && ch != '\r' && ch != '\n')
  2745.         {
  2746.             ch = *charPtr++;
  2747.             lineWidth++;
  2748.             
  2749.             if (ch == TAB)
  2750.                 cursorPt.h = destLeftSide + ((cursorPt.h - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  2751.             else if (ch != '\r' && ch != '\n')
  2752.                 cursorPt.h += theCharWidths[ch];
  2753.             
  2754.             if ((cursorPt.h >= rightSide && ch != ' ') || (!crOnly && lineWidth > maxLineWidth))
  2755.             {
  2756.                 maxRewind = charPtr - charBase - (**theTE32KHandle).lineStarts[firstLine];
  2757.                 oldCharPtr = charPtr;
  2758.                 
  2759.                 charPtr--;
  2760.                 maxRewind--;
  2761.                 
  2762.                 while (*charPtr != ' ' && maxRewind > 0)
  2763.                 {
  2764.                     charPtr--;
  2765.                     maxRewind--;
  2766.                 }
  2767.                 
  2768.                 if (maxRewind <= 0)
  2769.                     charPtr = oldCharPtr;
  2770.                 
  2771.                 else
  2772.                     charPtr++;
  2773.                 
  2774.                 charCount = 0;
  2775.             }
  2776.         }
  2777.     }
  2778.     
  2779.     return(charPtr - charBase);
  2780. }
  2781.  
  2782.  
  2783.  
  2784. #define    NUMTEMPLINES    32
  2785.  
  2786. static void    CalParagraph(long firstLine,TE32KHandle theTE32KHandle,long *theDeltaLines,long *theNumAffected)
  2787.  
  2788. {
  2789. register unsigned char    *charPtr;
  2790. register int            *theCharWidths;
  2791. register long            charCount,*lineStarts,*otherLine,i;
  2792. register long            crOnly,lineWidth,maxLineWidth;
  2793. register unsigned char    ch;
  2794. register long            nLines;
  2795. long                    maxLineStarts,sizeTE32KHandle,oldCharCount;
  2796. unsigned char            *charBase;
  2797. Point                    cursorPt;
  2798. int                        rightSide,destLeftSide,tabWidth,maxRewind;
  2799. unsigned char            *oldCharPtr;
  2800. long                    tempLineStarts[NUMTEMPLINES],oldNumLines,deltaLines;
  2801.     
  2802.     if ((**theTE32KHandle).crOnly)
  2803.     {
  2804.         *theDeltaLines = 0L;
  2805.         *theNumAffected = 0L;
  2806.         return;
  2807.     }
  2808.     
  2809.     deltaLines = 0L;
  2810.     
  2811.     oldNumLines = paraLines(firstLine,theTE32KHandle);
  2812.     
  2813.     for (i=0;i<oldNumLines && i <NUMTEMPLINES;i++)
  2814.         tempLineStarts[i] = (**theTE32KHandle).lineStarts[firstLine + i];
  2815.         
  2816.     sizeTE32KHandle  = GetHandleSize((Handle) theTE32KHandle);
  2817.     maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  2818.     
  2819.     nLines = 0;
  2820.     tempLineStarts[nLines] = (**theTE32KHandle).lineStarts[firstLine];
  2821.     
  2822.     crOnly = (**theTE32KHandle).crOnly;
  2823.     maxLineWidth = (**theTE32KHandle).maxLineWidth;
  2824.     lineWidth = 0;
  2825.     
  2826.     charBase = (unsigned char *) *((**theTE32KHandle).hText);
  2827.     charPtr = charBase + (**theTE32KHandle).lineStarts[firstLine];
  2828.     
  2829.     charCount = (**theTE32KHandle).teLength - (**theTE32KHandle).lineStarts[firstLine];
  2830.     ch = *charPtr;
  2831.     
  2832.     if (charCount > 0L)
  2833.     {
  2834.         rightSide = (int) ((**theTE32KHandle).destRect.right);
  2835.         destLeftSide = (int) ((**theTE32KHandle).destRect.left + 1L);
  2836.         cursorPt.h = destLeftSide;
  2837.         tabWidth = (long) (**theTE32KHandle).tabWidth;
  2838.         
  2839.         theCharWidths = (**theTE32KHandle).theCharWidths;
  2840.         
  2841.         ch = ' ';
  2842.         
  2843.         while (ch != '\r' && ch != '\n' && charCount--)
  2844.         {
  2845.             ch = *charPtr++;
  2846.             lineWidth++;
  2847.             
  2848.             if (ch != '\r' && ch != '\n')
  2849.             {
  2850.                 if (ch == TAB)
  2851.                     cursorPt.h = destLeftSide + ((cursorPt.h - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  2852.                 else
  2853.                     cursorPt.h += theCharWidths[ch];
  2854.                 
  2855.                 if ((cursorPt.h >= rightSide && ch != ' ') || (!crOnly && lineWidth > maxLineWidth))
  2856.                 {
  2857.                     maxRewind = charPtr - charBase - tempLineStarts[nLines];
  2858.                     oldCharPtr = charPtr;
  2859.                     oldCharCount = charCount;
  2860.                     
  2861.                     charPtr--;
  2862.                     charCount++;
  2863.                     maxRewind--;
  2864.                     
  2865.                     while (*charPtr != ' ' && maxRewind > 0)
  2866.                     {
  2867.                         charPtr--;
  2868.                         charCount++;
  2869.                         maxRewind--;
  2870.                     }
  2871.                     
  2872.                     if (maxRewind <= 0)
  2873.                     {
  2874.                         charPtr = oldCharPtr;
  2875.                         charCount = oldCharCount;
  2876.                     }
  2877.                     
  2878.                     else
  2879.                     {
  2880.                         charPtr++;
  2881.                         charCount--;
  2882.                     }
  2883.                     
  2884.                     nLines++;
  2885.                     
  2886.                     if (nLines < NUMTEMPLINES)
  2887.                     {
  2888.                         if (tempLineStarts[nLines] == charPtr - charBase)
  2889.                         {
  2890.                             oldNumLines = nLines;
  2891.                             goto STOPWRAPPING;
  2892.                         }
  2893.                         else
  2894.                             tempLineStarts[nLines] = charPtr - charBase;
  2895.                     }
  2896.                     
  2897.                     else
  2898.                         goto STOPWRAPPING;
  2899.                     
  2900.                     cursorPt.h = destLeftSide;
  2901.                     lineWidth = 0;
  2902.                 }
  2903.             }
  2904.         }
  2905.         
  2906.         nLines++;
  2907.         
  2908.         if (nLines < NUMTEMPLINES)
  2909.             tempLineStarts[nLines] = charPtr - charBase;
  2910.  
  2911. STOPWRAPPING:
  2912.  
  2913.         deltaLines = nLines - oldNumLines;
  2914.  
  2915.         if (nLines >= NUMTEMPLINES)
  2916.         {
  2917.             TE32KCalText(theTE32KHandle);
  2918.             deltaLines = (**theTE32KHandle).nLines - firstLine - oldNumLines;
  2919.         }
  2920.         
  2921.         else
  2922.         {
  2923.             if (deltaLines == 0L)
  2924.             {
  2925.                 for (i = 1;i <= nLines;i++)
  2926.                     (**theTE32KHandle).lineStarts[firstLine + i] = tempLineStarts[i];
  2927.             }
  2928.             
  2929.             else if (deltaLines < 0L)
  2930.             {
  2931.                 lineStarts = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  2932.                 
  2933.                 for (i = 1;i <= nLines;i++)
  2934.                     *(lineStarts++) = tempLineStarts[i];
  2935.                 
  2936.                 otherLine = &((**theTE32KHandle).lineStarts[firstLine + oldNumLines + 1L]);
  2937.                 i = (**theTE32KHandle).nLines - firstLine - oldNumLines + 1L;
  2938.                 
  2939.                 while (i--)
  2940.                     *(lineStarts++) = *(otherLine++);
  2941.                 
  2942.                 (**theTE32KHandle).nLines += deltaLines;
  2943.             }
  2944.             
  2945.             else
  2946.             {
  2947.                 if ((**theTE32KHandle).nLines + deltaLines >= maxLineStarts)
  2948.                 {
  2949.                     sizeTE32KHandle = (long) sizeof(TE32KRec) + (long) sizeof(long)*((**theTE32KHandle).nLines + deltaLines + EXTRALINESTARTS);
  2950.                     maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  2951.                     
  2952.                     SetHandleSize((Handle) theTE32KHandle,sizeTE32KHandle);
  2953.                     
  2954.                     if (MemError())
  2955.                     {
  2956.                         nLines = (**theTE32KHandle).nLines;
  2957.                         deltaLines = (**theTE32KHandle).nLines;
  2958.                         goto EXITPOINT;
  2959.                     }
  2960.                 }
  2961.                 
  2962.                 lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  2963.                 otherLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines + deltaLines]);
  2964.                 i = (**theTE32KHandle).nLines - firstLine - oldNumLines;
  2965.                 
  2966.                 while (i--)
  2967.                     *(otherLine--) = *(lineStarts--);
  2968.                     
  2969.                 for (i = nLines;i >= 0;i--)
  2970.                     *(otherLine--) = tempLineStarts[i];
  2971.                 
  2972.                 (**theTE32KHandle).nLines += deltaLines;
  2973.             }
  2974.         }
  2975.     }
  2976.     
  2977. EXITPOINT:
  2978.     *theNumAffected = nLines;
  2979.     *theDeltaLines = deltaLines;
  2980. }
  2981.  
  2982.  
  2983.  
  2984.  
  2985.  
  2986. static void updateLine(register long firstLine, TE32KHandle theTE32KHandle,int doFirst, LongRect *updateClipRect)
  2987. {
  2988. Rect            tempRect;
  2989. RgnHandle        updateRgn;
  2990. LongRect        updateRect;
  2991. LongPoint        selPt;
  2992. unsigned char    doWrap;
  2993. long            deltaLines,numAffected;
  2994. GrafPtr            oldPort;
  2995.  
  2996.  
  2997.     updateRect = (**theTE32KHandle).viewRect;
  2998.     updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2999.     updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  3000.     
  3001.     if (updateClipRect)
  3002.     {
  3003.         if (updateRect.top < updateClipRect->top)
  3004.             updateRect.top = updateClipRect->top;
  3005.         if (updateRect.bottom > updateClipRect->bottom)
  3006.             updateRect.bottom = updateClipRect->bottom;
  3007.         if (updateRect.left < updateClipRect->left)
  3008.             updateRect.left = updateClipRect->left;
  3009.         if (updateRect.right > updateClipRect->right)
  3010.             updateRect.right = updateClipRect->right;
  3011.     }
  3012.     
  3013.     doWrap = FALSE;
  3014.     
  3015.     if (firstLine > 0L && LineEndIndex(firstLine - 1L,theTE32KHandle) != (**theTE32KHandle).lineStarts[firstLine])
  3016.     {
  3017.         doWrap = TRUE;
  3018.         firstLine--;
  3019.     }
  3020.     
  3021.     else if (LineEndIndex(firstLine,theTE32KHandle) != (**theTE32KHandle).lineStarts[firstLine + 1L])
  3022.         doWrap = TRUE;
  3023.     
  3024.     
  3025.     if (!doWrap && doFirst)
  3026.         TE32KUpdate(&updateRect,theTE32KHandle);
  3027.     
  3028.     else if (doWrap)
  3029.     {
  3030.         CalParagraph(firstLine,theTE32KHandle,&deltaLines,&numAffected);
  3031.         
  3032.         if (deltaLines == 0L)
  3033.         {
  3034.             updateRect = (**theTE32KHandle).viewRect;
  3035.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  3036.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  3037.         }
  3038.         
  3039.         else if (deltaLines > 0L)
  3040.         {
  3041.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  3042.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine + numAffected - deltaLines) * (**theTE32KHandle).lineHeight;
  3043.             
  3044.             GetPort(&oldPort);
  3045.             SetPort((**theTE32KHandle).inPort);
  3046.     
  3047.             updateRgn = NewRgn();
  3048.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  3049.             DisposeRgn(updateRgn);
  3050.             
  3051.             SetPort(oldPort);
  3052.             
  3053.             updateRect = (**theTE32KHandle).viewRect;
  3054.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  3055.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  3056.         }
  3057.         
  3058.         else
  3059.         {
  3060.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  3061.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine - 1L + numAffected) * (**theTE32KHandle).lineHeight;
  3062.             
  3063.             GetPort(&oldPort);
  3064.             SetPort((**theTE32KHandle).inPort);
  3065.     
  3066.             updateRgn = NewRgn();
  3067.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  3068.             
  3069.             SetPort(oldPort);
  3070.             
  3071.             updateRect.left = (**updateRgn).rgnBBox.left;
  3072.             updateRect.top = (**updateRgn).rgnBBox.top;
  3073.             updateRect.right = (**updateRgn).rgnBBox.right;
  3074.             updateRect.bottom = (**updateRgn).rgnBBox.bottom;
  3075.             
  3076.             DisposeRgn(updateRgn);
  3077.             
  3078.             TE32KUpdate(&updateRect,theTE32KHandle);
  3079.             
  3080.             updateRect = (**theTE32KHandle).viewRect;
  3081.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  3082.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  3083.         }
  3084.         
  3085.         TE32KUpdate(&updateRect,theTE32KHandle);
  3086.     }
  3087.  
  3088.     TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  3089.     (**theTE32KHandle).selPoint = selPt;
  3090. }
  3091.  
  3092.  
  3093.  
  3094.  
  3095.  
  3096.  
  3097. static int    shiftKeyDown()
  3098. {
  3099. KeyMap    theKeyMap;
  3100.  
  3101.     GetKeys(theKeyMap);
  3102.     
  3103.     if (theKeyMap[1] & 0x01)
  3104.         return(TRUE);
  3105.     else
  3106.         return(FALSE);
  3107. }
  3108.  
  3109.  
  3110.  
  3111.  
  3112. static void MyClicker(void)
  3113. {
  3114. int            lineHeight;
  3115. Rect        viewRect;
  3116. Point        mousePoint;
  3117. RgnHandle    saveClip;
  3118. long        hDelta,vDelta;
  3119.  
  3120.     if (clickedTE32KH)
  3121.     {
  3122.         LongRectToRect(&((**clickedTE32KH).viewRect),&viewRect);
  3123.         lineHeight = (**clickedTE32KH).lineHeight;
  3124.     
  3125.         hDelta = 0L;
  3126.         vDelta = 0L;
  3127.         
  3128.         GetMouse(&mousePoint);
  3129.         
  3130.         if (!PtInRect(mousePoint,&viewRect))
  3131.         {
  3132.             if (mousePoint.v > viewRect.bottom && (**clickedTE32KH).viewRect.bottom < (**clickedTE32KH).destRect.top + (long) lineHeight * (**clickedTE32KH).nLines)
  3133.                 vDelta = -lineHeight;
  3134.             
  3135.             else if (mousePoint.v < viewRect.top && (**clickedTE32KH).viewRect.top > (**clickedTE32KH).destRect.top)
  3136.                 vDelta = lineHeight;
  3137.             
  3138.             
  3139.             if (mousePoint.h > viewRect.right && (**clickedTE32KH).viewRect.right < (**clickedTE32KH).destRect.right)
  3140.                 hDelta = -lineHeight;
  3141.             
  3142.             else if (mousePoint.h<viewRect.left && (**clickedTE32KH).viewRect.left > (**clickedTE32KH).destRect.left)
  3143.                 hDelta = lineHeight;
  3144.         }
  3145.         
  3146.         if (hDelta || vDelta)
  3147.         {
  3148.             saveClip = NewRgn();
  3149.             GetClip(saveClip);
  3150.             viewRect = (*((**clickedTE32KH).inPort)).portRect;
  3151.             ClipRect(&viewRect);
  3152.             
  3153.             TE32KScroll(hDelta,vDelta,clickedTE32KH);
  3154.             
  3155.             SetClip(saveClip);
  3156.             DisposeRgn(saveClip);
  3157.         }
  3158.     }
  3159. }
  3160.  
  3161.  
  3162.  
  3163.  
  3164. static void MyClickLoop(void)
  3165. {
  3166.     asm
  3167.     {
  3168.         movem.l        d1-d7/a0-a6,-(sp)
  3169.         jsr            MyClicker
  3170.         movem.l        (sp)+,d1-d7/a0-a6
  3171.         moveq.l        #1,d0
  3172.         rts
  3173.     }
  3174. }
  3175.  
  3176.  
  3177.  
  3178.  
  3179. void    TE32KAutoView(char autoView, TE32KHandle theTE32KHandle)
  3180. {
  3181.     if (theTE32KHandle)
  3182.     {
  3183.         if (!autoView)
  3184.             (**theTE32KHandle).clikLoop = 0L;
  3185.         else
  3186.             (**theTE32KHandle).clikLoop = (TE32KProcPtr) MyClickLoop;
  3187.     }
  3188. }
  3189.  
  3190.  
  3191.  
  3192.  
  3193.